首頁  >  文章  >  後端開發  >  php上傳圖片產生縮圖

php上傳圖片產生縮圖

WBOY
WBOY原創
2016-07-25 08:46:091239瀏覽
  1. function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){
  2. $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.');
  3. $type = preg_replace('@^. (?<=/)(. )$@', '', $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


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn