Home  >  Article  >  Backend Development  >  Upload images and generate thumbnails using ImageMagick

Upload images and generate thumbnails using ImageMagick

WBOY
WBOYOriginal
2016-07-25 08:45:431427browse
Using ImageMagick you can create thumbnails easily and faster, much easier than with PHP.
  1. // Location to upload main image:
  2. $mainDir = $_SERVER['DOCUMENT_ROOT'].'/images/l/';
  3. // Location to create the thumb image:
  4. $smalDir = $_SERVER['DOCUMENT_ROOT'].'/images/s/';
  5. // Command to use:
  6. $command = '/usr/bin/convert';
  7. // Thumbnail width:
  8. $size = 210;
  9. // Make sure we have an image:
  10. if(isset($_POST['submit'])){
  11. if(getimagesize($_FILES['photo']['tmp_name'])){
  12. $name = $_FILES ['photo']['name'];
  13. $uploadfile = $mainDir . $name;
  14. move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);
  15. $lrgImg = $mainDir . $name ;
  16. $smlImg = $smalDir . $name;
  17. $imageMagick = $command . " '". $lrgImg . "' -resize '$size' '" . $smlImg . "'";
  18. shell_exec($imageMagick);
  19. }
  20. header("Location: /test.php");
  21. exit;
  22. }else{
  23. ?>
  24. foreach(glob($smalDir.'*') as $img ){
  25. echo ' ';
  26. }
  27. }
  28. ?>
Copy code

Image upload, ImageMagick


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn