当前位置:Gxlcms > PHP教程 > php上传图片生成缩略图

php上传图片生成缩略图

时间:2021-07-01 10:21:17 帮助过:21人阅读

  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

人气教程排行