function coverImg($source,$target,$targetWidth,$targetHeight){
$system=explode(".",$source);
if (preg_match("/jpg|jpeg/",$system[count($system)-1])){
$src_img=imagecreatefromjpeg($source);
}
if (preg_match("/png/",$system[count($system)-1])){
$src_img=imagecreatefrompng($source);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) { //图片宽度大于高度
$thumb_h = $old_y*($targetWidth/$old_x);
$thumb_w = $targetWidth;
//$thumb_h=$targetHeight;
//$thumb_w=$old_x*($targetHeight/$old_y);
}
if ($old_x < $old_y) { //图片高度大于宽度
$thumb_w = $old_x*($targetHeight/$old_y);
$thumb_h = $targetHeight;
//$thumb_h=$old_y*($targetWidth/$old_x);
//$thumb_w=$targetWidth;
}
if ($old_x == $old_y) {
$thumb_w=$targetWidth;
$thumb_h=$targetHeight;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
imagejpeg($dst_img,$target);
imagedestroy($dst_img);
imagedestroy($src_img);
return true;
}
$system=explode(".",$source);
if (preg_match("/jpg|jpeg/",$system[count($system)-1])){
$src_img=imagecreatefromjpeg($source);
}
if (preg_match("/png/",$system[count($system)-1])){
$src_img=imagecreatefrompng($source);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) { //图片宽度大于高度
$thumb_h = $old_y*($targetWidth/$old_x);
$thumb_w = $targetWidth;
//$thumb_h=$targetHeight;
//$thumb_w=$old_x*($targetHeight/$old_y);
}
if ($old_x < $old_y) { //图片高度大于宽度
$thumb_w = $old_x*($targetHeight/$old_y);
$thumb_h = $targetHeight;
//$thumb_h=$old_y*($targetWidth/$old_x);
//$thumb_w=$targetWidth;
}
if ($old_x == $old_y) {
$thumb_w=$targetWidth;
$thumb_h=$targetHeight;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
imagejpeg($dst_img,$target);
imagedestroy($dst_img);
imagedestroy($src_img);
return true;
}
写的比较简单,可以将上传的图片转换为jpg格式,并且可以控制最大宽度和最大高度
2018年10月25日 22:26
I have learnt more about coding here. In https://www.topdissertations.org/wordsdoctorate-review/, I have downloaded many materials on programming. I am really glad to get this site.