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

这篇文章主要为大家详细介绍了php实现图片上传并利用ImageMagick生成缩略图的相关资料,需要的朋友可以参考下。

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

  1. <?php
  2. // Location to upload main image:
  3. $mainDir = $_SERVER['DOCUMENT_ROOT'].'/images/l/';
  4. // Location to create the thumb image:
  5. $smalDir = $_SERVER['DOCUMENT_ROOT'].'/images/s/';
  6. // Command to use:
  7. $command = '/usr/bin/convert';
  8. // Thumbnail width:
  9. $size = 210;
  10. // Make sure we have an image:
  11. if(isset($_POST['submit'])){
  12. if(getimagesize($_FILES['photo']['tmp_name'])){
  13. $name = $_FILES['photo']['name'];
  14. $uploadfile = $mainDir . $name;
  15. move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);
  16. $lrgImg = $mainDir . $name;
  17. $smlImg = $smalDir . $name;
  18. $imageMagick = $command . " '". $lrgImg . "' -resize '$size' '" . $smlImg . "'";
  19. shell_exec($imageMagick);
  20. }
  21. header("Location: /test.php");
  22. exit;
  23. }else{
  24. ?>
  25. <form action=" <?php echo $_SERVER['PHP_SELF']; ?> " method="post" enctype="multipart/form-data">
  26. <p><input type="file" name="photo" /></p>
  27. <p><input type="submit" value="Upload!" name="submit" /></p>
  28. </form>
  29. <?php
  30. foreach(glob($smalDir.'*') as $img){
  31. echo ' <img src="'.str_replace($_SERVER['DOCUMENT_ROOT'], '',$img).'" /> ';
  32. }
  33. }
  34. ?>

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