PHP imagecopyresampled比率、大小调整和裁剪问题
好的,我正在写一个图片上传,我们想强制图片为795x440px。它们可以被重新压缩,但它们必须保持长宽比,这样它们也可以被裁剪 新图像的大小是正确的,但是原始文件中的裁剪图像的比例是错误的,尝试了一些不同的方法,但无法获得正确的图像 我正在测试的图像,原始文件 种植的结果 我如何才能做到这一点,使图像始终获得最佳大小,并裁剪其余部分PHP imagecopyresampled比率、大小调整和裁剪问题,php,image,crop,Php,Image,Crop,好的,我正在写一个图片上传,我们想强制图片为795x440px。它们可以被重新压缩,但它们必须保持长宽比,这样它们也可以被裁剪 新图像的大小是正确的,但是原始文件中的裁剪图像的比例是错误的,尝试了一些不同的方法,但无法获得正确的图像 我正在测试的图像,原始文件 种植的结果 我如何才能做到这一点,使图像始终获得最佳大小,并裁剪其余部分 list($width, $height) = getimagesize($save_dir); $prop = (795 / $width); $heigh
list($width, $height) = getimagesize($save_dir);
$prop = (795 / $width);
$height = floor($height * $prop);
$new_image = imagecreatetruecolor(795, 440);
$bgColor = imagecolorallocate($new_image, 255,255,255) or die("Couldn't allocate color");
imagefill($new_image , 0,0 , $bgColor) or die("Couldnt fill with color");
imagecopyresampled($new_image,$source_image,0,0,0,0,795,440,795,$height);
imagejpeg($new_image,$new_directory,100);
我是这样做的:
public function cropImage($nw, $nh, $source, $stype, $dest) {
list($w, $h) = getimagesize($source);
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
case 'jpeg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$white = imagecolorallocate($dimg, 255, 255, 255);
imagefill($dimg, 1, 1, $white);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w > $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg, $simg, -$int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h);
} elseif(($w < $h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
if(imagejpeg($dimg, $dest, 70))
return true;
else
die("cropImage: error.");
}
public function cropImage($nw、$nh、$source、$stype、$dest){
列表($w,$h)=getimagesize($source);
交换机($stype){
案例“gif”:
$simg=imagecreatefromformgif($source);
打破
案件‘jpg’:
案例“jpeg”:
$simg=imagecreatefromjpeg($source);
打破
案例“png”:
$simg=imagecreatefrompng($source);
打破
}
$dimg=ImageCreateTureColor($nw,$nh);
$white=imagecolorallocate($dimg,255,255,255);
imagefill($dimg,1,1,$white);
$wm=$w/$nw;
$hm=$h/$nh;
$h_高度=$nh/2;
$w_高度=$nw/2;
如果($w>$h){
$adjusted_width=$w/$hm;
$half_width=$adjusted_width/2;
$int_width=$half_width-$w_height;
imagecopyresampled($dimg,$simg,$int_width,0,0,$adjusted_width,$nh,$w,$h);
}其他($w<$h)| |($w==h)){
$adjusted_height=$h/$wm;
$half_height=$adjusted_height/2;
$int_height=$half_height-$h_height;
imagecopyresampled($dimg,$simg,0,$int_height,0,0,$nw,$adjusted_height,$w,$h);
}否则{
imagecopyresampled($dimg、$simg、0,0,0、$nw、$nh、$w、$h);
}
if(图像JPEG($dimg,$dest,70))
返回true;
其他的
死亡(“cropImage:错误”);
}
我是这样做的:
public function cropImage($nw, $nh, $source, $stype, $dest) {
list($w, $h) = getimagesize($source);
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
case 'jpeg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$white = imagecolorallocate($dimg, 255, 255, 255);
imagefill($dimg, 1, 1, $white);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w > $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg, $simg, -$int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h);
} elseif(($w < $h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
if(imagejpeg($dimg, $dest, 70))
return true;
else
die("cropImage: error.");
}
public function cropImage($nw、$nh、$source、$stype、$dest){
列表($w,$h)=getimagesize($source);
交换机($stype){
案例“gif”:
$simg=imagecreatefromformgif($source);
打破
案件‘jpg’:
案例“jpeg”:
$simg=imagecreatefromjpeg($source);
打破
案例“png”:
$simg=imagecreatefrompng($source);
打破
}
$dimg=ImageCreateTureColor($nw,$nh);
$white=imagecolorallocate($dimg,255,255,255);
imagefill($dimg,1,1,$white);
$wm=$w/$nw;
$hm=$h/$nh;
$h_高度=$nh/2;
$w_高度=$nw/2;
如果($w>$h){
$adjusted_width=$w/$hm;
$half_width=$adjusted_width/2;
$int_width=$half_width-$w_height;
imagecopyresampled($dimg,$simg,$int_width,0,0,$adjusted_width,$nh,$w,$h);
}其他($w<$h)| |($w==h)){
$adjusted_height=$h/$wm;
$half_height=$adjusted_height/2;
$int_height=$half_height-$h_height;
imagecopyresampled($dimg,$simg,0,$int_height,0,0,$nw,$adjusted_height,$w,$h);
}否则{
imagecopyresampled($dimg、$simg、0,0,0、$nw、$nh、$w、$h);
}
if(图像JPEG($dimg,$dest,70))
返回true;
其他的
死亡(“cropImage:错误”);
}
您可以尝试此功能
function thumb($file, $save, $width, $height)
{
@unlink($save);
if (!$infos = @getimagesize($file)) {
return false;
}
$iWidth = $infos[0];
$iHeight = $infos[1];
$iRatioW = $width / $iWidth;
$iRatioH = $height / $iHeight;
$iNewW = $width;
$iNewH=($iHeight/$iWidth)*$iNewW;
//$iNewH = $height;
//Don't resize images which are smaller than thumbs
if ($infos[0] < $width && $infos[1] < $height) {
$iNewW = $infos[0];
$iNewH = $infos[1];
}
if($infos[2] == 1) {
$imgA = imagecreatefromgif($file);
$imgB = imagecreate($iNewW,$iNewH);
if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
$transcolorindex = imagecolortransparent($imgA);
//transparent color exists
if($transcolorindex >= 0 ) {
$transcolor = imagecolorsforindex($imgA, $transcolorindex);
$transcolorindex = imagecolorallocate($imgB, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
imagefill($imgB, 0, 0, $transcolorindex);
imagecolortransparent($imgB, $transcolorindex);
//fill white
} else {
$whitecolorindex = @imagecolorallocate($imgB, 255, 255, 255);
imagefill($imgB, 0, 0, $whitecolorindex);
}
//fill white
} else {
$whitecolorindex = imagecolorallocate($imgB, 255, 255, 255);
imagefill($imgB, 0, 0, $whitecolorindex);
}
imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
imagegif($imgB, $save);
} elseif($infos[2] == 2) {
$imgA = imagecreatefromjpeg($file);
$imgB = imagecreatetruecolor($iNewW,$iNewH);
imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
imagejpeg($imgB, $save);
} elseif($infos[2] == 3) {
/*
* Image is typ png
*/
$imgA = imagecreatefrompng($file);
$imgB = imagecreatetruecolor($iNewW, $iNewH);
imagealphablending($imgB, false);
imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
imagesavealpha($imgB, true);
imagepng($imgB, $save);
} else {
return false;
}
return true;
}
thumb($source,$target_full_path,'337','208');
function thumb($file、$save、$width、$height)
{
@取消链接($save);
如果(!$infos=@getimagesize($file)){
返回false;
}
$iWidth=$infos[0];
$iHeight=$infos[1];
$iRatioW=$width/$iWidth;
$iRatioH=$height/$iHeight;
$iNewW=$width;
$iNewH=($iHeight/$iWidth)*$iNewW;
//$iNewH=$height;
//不要调整比拇指小的图像的大小
如果($infos[0]<$width&$infos[1]<$height){
$iNewW=$infos[0];
$iNewH=$infos[1];
}
如果($infos[2]==1){
$imgA=imagecreatefromgif($file);
$imgB=imagecreate($iNewW,$iNewH);
if(函数_存在('imagecolorsforindex')&函数_存在('imageColorsTransparent')){
$transcolorindex=imagecolortransparent($imgA);
//存在透明颜色
如果($transcolorindex>=0){
$transcolor=imagecolorsforindex($imgA,$transcolorindex);
$transcolorindex=imagecolorallocate($imgB、$transcolor['red']、$transcolor['green']、$transcolor['blue']);
imagefill($imgB,0,0,$transcolorindex);
imagecolortransparent($imgB,$transcolorindex);
//填补空白
}否则{
$whitecolorindex=@imagecolorallocate($imgB,255,255,255);
imagefill($imgB,0,0,$whitecolorindex);
}
//填补空白
}否则{
$whitecolorindex=imagecolorallocate($imgB,255,255,255);
imagefill($imgB,0,0,$whitecolorindex);
}
imagecopyresampled($imgB、$imgA、0、0、0、$iNewW、$iNewH、$infos[0]、$infos[1]);
imagegif($imgB,$save);
}elseif($infos[2]==2){
$imgA=imagecreatefromjpeg($file);
$imgB=ImageCreateTureColor($iNewW,$iNewH);
imagecopyresampled($imgB、$imgA、0、0、0、$iNewW、$iNewH、$infos[0]、$infos[1]);
imagejpeg($imgB,$save);
}elseif($infos[2]==3){
/*
*图像是典型的png
*/
$imgA=imagecreatefrompng($file);
$imgB=ImageCreateTureColor($iNewW,$iNewH);
imagealphablending($imgB,false);
imagecopyresampled($imgB、$imgA、0、0、0、$iNewW、$iNewH、$infos[0]、$infos[1]);
imagesavealpha($imgB,true);
imagepng($imgB,$save);
}否则{
返回false;
}