Note: Only one of $maxwidth and $maxheight can be passed. If the maximum width is passed, the height will be automatically calculated. If the maximum height is passed, the width will be automatically calculated. * Return value: If the creation is successful, return the address where the file is saved, otherwise return false
Note: Only one of $maxwidth and $maxheight can be passed. If the maximum width is passed, the height will be automatically calculated. If the maximum height is passed, the width will be automatically calculated
* Return value: If the creation is successful, return the address where the file is saved, otherwise return false
/***************************************************** ************************
* Function name: createSmallImg()
* Function description: Create proportional pictures
* Input parameters:
$dir save path
$source_img Original image name
$small_ex thumbnail file name suffix
$maxwidth maximum width
$maxheight maximum height
* Note: Only one of $maxwidth and $maxheight can be passed. If the maximum width is passed, the height will be automatically calculated. If the maximum height is passed, the width will be automatically calculated
* Return value: If the creation is successful, return the address where the file is saved, otherwise return false
*Written by: Li Xiaoyu
* Writing time: 2011/8/18
*************************************************** ************************/
function createSmallImg($dir,$source_img,$small_ex="_s",$maxwidth='',$maxheight='') {
if(!empty($maxwidth) && !empty($maxheight)) {
return false;
}
$img_name=substr($source_img,0,-4);
$img_ex = strtolower(substr(strrchr($source_img,"."),1));
/*This section of the comment is used to display images directly on the browser
$im=imagecreatefromjpeg($file);
header("Content-type: image/jpeg");
imagejpeg($im);*/
switch($img_ex) {
case "jpg":
$src_img=imagecreatefromjpeg($dir.$source_img);
Break;
case "gif":
$src_img=imagecreatefromgif($dir.$source_img);
Break;
case "png":
$src_img=imagecreatefrompng($dir.$source_img);
Break;
}
$old_width=imagesx($src_img);
$old_height=imagesy($src_img);
if(!empty($maxheight) && $old_height>=$maxheight) {
$new_height=$maxheight;
$new_width=round(($old_width*$new_height)/$old_height);
} elseif(!empty($maxwidth) && $old_width>=$maxwidth) {
$new_width=$maxwidth;
$new_height=round(($old_height*$new_width)/$old_width);
}
if(!empty($new_width) || !empty($new_height)) {
if($img_ex=="jpg" || $img_ex=="png") {
$dst_img=imagecreatetruecolor($new_width,$new_height);
} else {
$dst_img=imagecreate($new_width,$new_height);
}
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);
$smallname=$dir.$img_name.$small_ex.".".$img_ex;
switch($img_ex) {
case "jpg":
Imagejpeg($dst_img,$smallname,100);
Break;
case "gif":
Imagegif($dst_img,$smallname);
Break;
case "png":
Imagepng($dst_img,$smallname);
Break;
}
}
return $smallname;
}