php实现图片上传并利用ImageMagick生成缩略图

时间:2021-05-26

使用ImageMagick,您可以轻松,更快地创建缩略图,比使用PHP的容易得多。

<?php// Location to upload main image:$mainDir = $_SERVER['DOCUMENT_ROOT'].'/images/l/';// Location to create the thumb image:$smalDir = $_SERVER['DOCUMENT_ROOT'].'/images/s/';// Command to use:$command = '/usr/bin/convert';// Thumbnail width:$size = 210;// Make sure we have an image:if(isset($_POST['submit'])){if(getimagesize($_FILES['photo']['tmp_name'])){$name = $_FILES['photo']['name'];$uploadfile = $mainDir . $name;move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);$lrgImg = $mainDir . $name;$smlImg = $smalDir . $name;$imageMagick = $command . " '". $lrgImg . "' -resize '$size' '" . $smlImg . "'";shell_exec($imageMagick);}header("Location: /test.php");exit;}else{?><form action=" <?php echo $_SERVER['PHP_SELF']; ?> " method="post" enctype="multipart/form-data"><p><input type="file" name="photo" /></p><p><input type="submit" value="Upload!" name="submit" /></p></form><?phpforeach(glob($smalDir.'*') as $img){echo ' <img src="'.str_replace($_SERVER['DOCUMENT_ROOT'], '',$img).'" /> ';}}?>

希望本文所述对大家学习php程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章