php 上传图片并生成缩略图的简单示例

前端之家收集整理的这篇文章主要介绍了php 上传图片并生成缩略图的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP上传图片生成缩略图,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 上传图片生成缩略图
 *
 * @param 
 * @author 编程之家 jb51.cc jb51.cc
 **/
function createThumbnail($imageDirectory,$imageName,$thumbDirectory,$thumbWidth,$quality){ 
    $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); 
    $type = preg_replace('@^.+(?<=/)(.+)$@','$1',$details['mime']); 
    eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); 
    $thumbHeight = $details[1] * ($thumbWidth / $details[0]); 
    $thumbImg = imagecreatetruecolor($thumbWidth,$thumbHeight); 
    imagecopyresampled($thumbImg,$srcImg,$thumbHeight,$details[0],$details[1]); 
    eval('image'.$type.'($thumbImg,"$thumbDirectory/$imageName"'. 
    (($type=='jpeg')?',$quality':'').');'); 
    imagedestroy($srcImg); 
    imagedestroy($thumbImg); 
}

foreach ($_FILES["pictures"]["error"] as $key => $error) { 
   if ($error == UPLOAD_ERR_OK) { 
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; 
       $name = $_FILES["pictures"]["name"][$key]; 
       move_uploaded_file($tmp_name,"data/$name"); 
       createThumbnail("/location/of/main/image",$name,"/location/to/store/thumb",120,80); 
       //120 = thumb width  ::  80 = thumb quality (1-100) 
   } 
} 


/***   代码来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/529012.html

猜你在找的PHP相关文章