首頁 > 後端開發 > php教程 > php上傳圖片產生縮圖

php上傳圖片產生縮圖

WBOY
發布: 2016-07-25 08:46:09
原創
1295 人瀏覽過
  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


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板