php 图片重定像素,在PHP中重新定义图像高度

这段博客介绍了如何在图像上传过程中确保图片的宽度和高度不超过115px。通过检查图像的原始尺寸,根据宽高比调整长边至115px,并相应缩放短边,以保持比例。代码示例展示了如何使用PHP的GD库进行图像尺寸调整,包括创建新图像、计算新尺寸并使用imagecopyresampled函数进行图像缩放,最后保存为PNG格式。
摘要由CSDN通过智能技术生成

如果你的意思是他们不能大于115x115。你必须经历的过程是这样的:

如果宽度大于115,则将宽度设置为115px,将height设置为height / original_width * 115;

我有一些来自图像上传器脚本的代码来执行此操作

list($Width, $Height) = getimagesize($row["img_file"]);

if(($Width > $Height) && ($Width > 115))

{

$Height = ceil(($Height / $Width) * 115) ;

$Width = 115;

}

elseif(($Height > $Width) && ($Height > 115))

{

$Width = ceil(($Width / $Height)* 115);

$Height = 115;

}

elseif(($Height > 115) && ($Height == $Width))

{

$Height = 115;

$Width = 115;

}获得正确的尺寸后,可以使用imagecreatefromjpeg / png / gif然后使用imagecopyresampled调整图像大小。

图像上载脚本的另一部分代码,使用计算的尺寸从URL调整图像大小

if ($extension == "png")

{

$image = imagecreatefrompng($URLofImage)or die("Width Of Image: ".$widthofImage." Height Of Image: ".$heightofImage." Height: ".$height." Width: ".$width);

$image_p = imagecreatetruecolor($widthofImage, $heightofImage)or die("Width Of Image: ".$widthofImage." Height Of Image: ".$heightofImage." Height: ".$height." Width: ".$width);

imagealphablending($image_p, false);

$color = imagecolorallocatealpha($image_p, 0, 0, 0, 0);

imagesavealpha($image_p, true);

list($width, $height) = getimagesize($URLofImage);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $widthofImage, $heightofImage, $width, $height);

imagepng($image_p, "./images/thumbs/".$name, 9);

return ("./images/thumbs/".$name);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值