php上传图片生成缩略图

原创
2016-07-25 08:46:09 939浏览
  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


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:推荐20个近段时间很流行的重量级PHP框架 下一条:PHP去除多余空格 多个连续空格只保留一个

相关文章

查看更多