php thumbnail creation after uploading file
Hi friends what is the best way to create thumbnail on the fly af开发者_Python百科ter uploading an image ?
move_uploaded_file($source, $dest)
//uploaded successfully
//what will be code to generate thumbnail ?
TRY THIS...
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
I often make use of Symfony's sfThumbnailPlugin; it works well standalone as well as part of the framework. Additionally I usually use the ImageMagick adaptor for the extra filetypes and image operations it can execute.
精彩评论