【发布时间】:2023-03-21 00:56:01
【问题描述】:
编辑:刚结束使用 Image Magick 并修复了它。
长话短说,我正在尝试使用 PHP 和 jQuery 的组合从 Wikipedia 中提取有关一系列学校的一些基本信息。部分信息是学校的标志或标志,很容易在元素列表中找到。
问题在于尝试在 PHP 中对图像进行一些调整。我知道图像存在于目标 URL(位于不同的域中,如果有帮助的话)并且它是我想要的,但某些图像看起来像这样:
这是原图:
所有文件类型中的其他文件都非常好。
该部分的代码如下:
$ext = end(explode('.', $image));
if($ext == 'png') {
$img = imagecreatefrompng($image);
}
else if($ext == 'jpeg' || $ext == 'jpg') {
$img = imagecreatefromjpeg($image);
}
else if($ext == 'gif') {
$img = imagecreatefromgif($image);
}
else $img = false;
if($img) {
$raw_x = imagesx($img);
$raw_y = imagesy($img);
if($raw_x > $raw_y && $raw_x > 500)
{
$y = (500 / $raw_x) * $raw_y;
$tmp_img = imagecreatetruecolor(500, $y);
$white = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill($tmp_img, 0, 0, $white);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, 500, $y, $raw_x, $raw_y);
$img = $tmp_img;
}
else if($raw_y > 500)
{
$x = (500 / $raw_y) * $raw_x;
$tmp_img = imagecreatetruecolor($x, 500);
$white = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill($tmp_img, 0, 0, $white);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $x, 500, $raw_x, $raw_y);
$img = $tmp_img;
}
if(!file_exists("../images/schools/" . $id)) mkdir("../images/schools/" . $id, 0755, true);
imagejpeg($img, "../images/schools/" . $id . "/photo.jpg", 100);
}
我已经在这几天了,我不知道出了什么问题,我希望有一双全新的眼睛能够看到我看不到的东西
【问题讨论】:
-
变形后的图片都是同一种文件类型吗?
-
在
imagecopyresampled期间不缩放它看起来是否正确? -
是
image magick和选项吗?我想这只是评论,但我总是有更好的运气execing 在我的脚本中调用Image Magick而不是尝试使用php函数。 -
您可能对图像的 Alpha 层有问题。你检查过这个页面上的 cmets 吗? php.net/manual/en/function.imagecreatefrompng.php
-
@chiliNUT - Image Magick 工作顺利,非常感谢!