php gd 缩小,PHP / GD – 裁剪和调整图像大小

我编写了一个函数,将图像裁剪为给定的宽高比,最后调整大小并将其输出为JPG:

function Image($image, $crop = null, $size = null)

{

$image = ImageCreateFromString(file_get_contents($image));

if (is_resource($image) === true)

{

$x = 0;

$y = 0;

$width = imagesx($image);

$height = imagesy($image);

/*

CROP (Aspect Ratio) Section

*/

if (is_null($crop) === true)

{

$crop = array($width, $height);

}

else

{

$crop = array_filter(explode(':', $crop));

if (empty($crop) === true)

{

$crop = array($width, $height);

}

else

{

if ((empty($crop[0]) === true) || (is_numeric($crop[0]) === false))

{

$crop[0] = $crop[1];

}

else if ((empty($crop[1]) === true) || (is_numeric($crop[1]) === false))

{

$crop[1] = $crop[0];

}

}

$ratio = array

(

0 => $width / $height,

1 => $crop[0] / $crop[1],

);

if ($ratio[0] > $ratio[1])

{

$width = $height * $ratio[1];

$x = (imagesx($image) - $width) / 2;

}

else if ($ratio[0] < $ratio[1])

{

$height = $width / $ratio[1];

$y = (imagesy($image) - $height) / 2;

}

/*

How can I skip (join) this operation

with the one in the Resize Section?

*/

$result = ImageCreateTrueColor($width, $height);

if (is_resource($result) === true)

{

ImageSaveAlpha($result, true);

ImageAlphaBlending($result, false);

ImageFill($result, 0, 0, ImageColorAllocateAlpha($result, 255, 255, 255, 127));

ImageCopyResampled($result, $image, 0, 0, $x, $y, $width, $height, $width, $height);

$image = $result;

}

}

/*

Resize Section

*/

if (is_null($size) === true)

{

$size = array(imagesx($image), imagesy($image));

}

else

{

$size = array_filter(explode('x', $size));

if (empty($size) === true)

{

$size = array(imagesx($image), imagesy($image));

}

else

{

if ((empty($size[0]) === true) || (is_numeric($size[0]) === false))

{

$size[0] = round($size[1] * imagesx($image) / imagesy($image));

}

else if ((empty($size[1]) === true) || (is_numeric($size[1]) === false))

{

$size[1] = round($size[0] * imagesy($image) / imagesx($image));

}

}

}

$result = ImageCreateTrueColor($size[0], $size[1]);

if (is_resource($result) === true)

{

ImageSaveAlpha($result, true);

ImageAlphaBlending($result, true);

ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255));

ImageCopyResampled($result, $image, 0, 0, 0, 0, $size[0], $size[1], imagesx($image), imagesy($image));

header('Content-Type: image/jpeg');

ImageInterlace($result, true);

ImageJPEG($result, null, 90);

}

}

return false;

}

?>

该功能按预期工作,但我正在创建一个非必需的GD图像资源,我该如何解决它?我试过加入两个电话,但我必须做一些错误的计算.

/*

Usage Examples

*/

Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', '1:1', '600x');

Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', '2:1', '600x');

Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', '2:', '250x300');

?>

非常感谢任何帮助,谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值