Home  >  Article  >  Backend Development  >  PHP uploads images to generate thumbnails

PHP uploads images to generate thumbnails

WBOY
WBOYOriginal
2016-07-25 08:46:091235browse
  1. function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){
  2. $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.');
  3. $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
  4. eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
  5. $thumbHeight = $details[1] * ($thumbWidth / $details[0]);
  6. $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
  7. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,
  8. $details[0], $details[1]);
  9. eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.
  10. (($type=='jpeg')?', $quality':'').');');
  11. imagedestroy($srcImg);
  12. imagedestroy($thumbImg);
  13. }
  14. foreach ($_FILES["pictures"]["error"] as $key => $error) {
  15. if ($error == UPLOAD_ERR_OK) {
  16. $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
  17. $name = $_FILES["pictures"]["name"][$key];
  18. move_uploaded_file($tmp_name, "data/$name");
  19. createThumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80);
  20. //120 = thumb width :: 80 = thumb quality (1-100)
  21. }
  22. }
  23. ?>
复制代码

上传图片, php


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