门户php方块,php-调整大小的图像中有黑色方块

我正在尝试完成此功能,除了一个小问题外,它似乎工作得很好-似乎将图像定位在离左侧太远的位置,然后用黑色填充其余空间.

我想做的是让此函数将图像调整为指定的$thumb_w,如果调整高度后最终的高度大于$thumb_h,则仅裁剪底部.

这是我的功能代码:

function resize_upload ($tmp, $thumb_w, $thumb_h, $img_name, $img_ext, $img_path)

{

if ($img_ext == 'jpg' || $img_ext == 'jpeg' || $img_ext == 'png' || $img_ext == 'gif')

{

if ($img_ext == 'jpg' || $img_ext == 'jpeg')

$source_img = imagecreatefromjpeg($tmp);

else if ($img_ext=='png')

$source_img = imagecreatefrompng($tmp);

else

$source_img = imagecreatefromgif($tmp);

$orig_w = imagesx($source_img);

$orig_h = imagesy($source_img);

$w_ratio = ($thumb_w / $orig_w);

$h_ratio = ($thumb_h / $orig_h);

if ($orig_w > $orig_h )

{

$crop_w = round($orig_w * $h_ratio);

$crop_h = $thumb_h;

$src_x = ceil( ( $orig_w - $thumb_w ) / 2 );

$src_y = 0;

}

elseif ($orig_w < $orig_h )

{

$crop_h = round($orig_h * $w_ratio);

$crop_w = $thumb_w;

$src_x = 0;

$src_y = ceil( ( $orig_h - $thumb_h ) / 2 );

}

else

{

$crop_w = $thumb_w;

$crop_h = $thumb_h;

$src_x = 0;

$src_y = 0;

}

$thumb_img = imagecreatetruecolor($thumb_w,$thumb_h);

imagecopyresampled($thumb_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);

imagejpeg($thumb_img, $img_path.'/'.$img_name.'.'.$img_ext, 100);

imagedestroy($thumb_img);

imagedestroy($source_img);

}

}

这是我如何调用函数的继承人:

resize_upload ($_FILES['image_main']['tmp_name'], 556, 346, $img_name, $img_ext, '../wp-content/themes/my-theme/images/projects');

这是函数执行其操作后图像最终看起来的样子:

看到右侧的黑色?它可能是我不知道的一些数学问题.任何帮助将不胜感激.

解决方法:

使用来自帖子$thumb_w = 556,$thumb_h = 346的相同变量,我们假设发送的图像尺寸完全相同,因此不需要调整大小(556×346).

$orig_w = 556;

$orig_h = 346;

$w_ratio = 1;

$h_ratio = 1;

if (556 > 346) //true

{

$crop_w = round(556 * 1); // 556

$crop_h = 346;

$src_x = ceil( ( 556 - 346 ) / 2 ); // ceil( 210 / 2 ) == 105;

$src_y = 0;

}

...

$thumb_img = imagecreatetruecolor(556, 346);

imagecopyresampled($thumb_img, $source_img, 0, 0, 105, 0, 556, 346, 556, 346);

因此,您的代码从源图像的x = 105开始,并尝试向其右移556像素,但在该点之后仅存在451像素.因此,如果我发送的是556×346图像,它将复制图像水平部分的像素105到556的宽度,然后复制垂直部分的0到346.因此,图像的整个垂直部分都显示出来,但不是全部宽度都显示出来.

我确定如果对高度大于宽度的图像进行相同的计算,则会在图像底部出现黑色空间而遇到同样的问题.

提示:在编写公式和其他需要大量计算的事物时,请首先使用最简单的数字进行处理.如果这些都不起作用,那么您显然做错了什么.

标签:image,image-processing,php

来源: https://codeday.me/bug/20191101/1986187.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值