嗨朋友上传图片后动态创建缩略图的最佳方法是什么?
move_uploaded_file($source, $dest)
//uploaded successfully
//what will be code to generate thumbnail ?
答案 0 :(得分:1)
尝试这个......
function create_img($file_name,$dir,$dest_dir,$maxwidth,$maxheight){
/* adjust server memory size, file upload size, and post size
* htaccess file:
* php_value post_max_size 16M
* php_value upload_max_filesize 6M
* script:
* ini_set('memory_limit', '100M'); //handle large images
*/
ini_set('memory_limit', '100M');
if(file_exists($dir)){
$file = "$dir/$file_name";
if(!file_exists($dest_dir.$file_name)){
list($orig_width, $orig_height, $type) = getimagesize($file);
//calculate dimensions
if ($orig_width > $maxwidth){
$height = $maxwidth * ($orig_height / $orig_width);
$width = $maxwidth;
if($height>$maxheight){
$height = $maxheight;
$width = $maxheight * ( $orig_width / $orig_height);
}
}
elseif($orig_height > $maxheight){
$height = $maxheight;
$width = $maxheight * ($orig_width / $orig_height);
if($width>$maxwidth){
$height = $maxwidth * ($orig_height / $orig_width);
$width = $maxwidth;
}
}
else{
$width = $orig_width;
$height = $orig_height;
}
$obr_p = @imagecreatetruecolor($width, $height);
if ($type == 1) {
$obr = @imagecreatefromgif($file);
imagecopyresampled($obr_p, $obr, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
imagegif($obr_p, "$dest_dir/$file_name");
}
else if($type == 2) {
$obr = imagecreatefromjpeg($file);
imagecopyresampled($obr_p, $obr, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
imagejpeg($obr_p, "$dest_dir/$file_name",100);
}
else if($type == 3){
$obr = @imagecreatefrompng($file);
imagecopyresampled($obr_p, $obr, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
imagepng($obr_p, "$dest_dir/$file_name");
}
else{
return FALSE;
}
}// if exist
else{
return FALSE;
}
}//if exist dir
else{
return FALSE;
}
}//end create_img
答案 1 :(得分:0)
我经常使用Symfony的sfThumbnailPlugin;它可以独立运行,也可以作为框架的一部分。另外,我通常使用ImageMagick适配器来执行额外的文件类型和图像操作。