PHP GD imagecopy() 上的图像损坏

发布于 2024-10-28 10:55:27 字数 1757 浏览 5 评论 0原文

我在使用 PHP 5.3.3 时遇到了一些奇怪的情况,我正在尝试向图像添加水印。

$body = @imagecreatefromstring($image_data['body']);
imagejpeg($body, null, 85);

返回:https://i.sstatic.net/KJjDi.jpg

$body   = @imagecreatefromstring($image_data['body']);
$logo   = @imagecreatefrompng(APP_ROOT . self::WATERMARK_PATH);

$body_width     = (int) @imagesx($body);
$body_height    = (int) @imagesy($body);

$logo_width     = (int) @imagesx($logo);
$logo_height    = (int) @imagesy($logo);

$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);

imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);

imagejpeg($image, null, 85);

返回:https://i.sstatic.net/nwtqZ.jpg

buuuuuuuuut......如果我添加身体(猫)到图像...

$body   = @imagecreatefromstring($image_data['body']);
$logo   = @imagecreatefrompng(APP_ROOT . self::WATERMARK_PATH);

$body_width     = (int) @imagesx($body);
$body_height    = (int) @imagesy($body);

$logo_width     = (int) @imagesx($logo);
$logo_height    = (int) @imagesy($logo);

$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);

imagecopy(
    $image, $body, 
    1, 1, 
    0, 0, $body_width, $body_height
);

imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);

imagejpeg($image, null, 85);

结果: https://i.sstatic.net/ Xeb73.jpg

正如您在最后一张中看到的那样,图像的底部已损坏或发生了其他情况……发生了什么?

I'm experiencing some weirdness with PHP 5.3.3, i'm trying to add a watermark to an image.

$body = @imagecreatefromstring($image_data['body']);
imagejpeg($body, null, 85);

returns in: https://i.sstatic.net/KJjDi.jpg

$body   = @imagecreatefromstring($image_data['body']);
$logo   = @imagecreatefrompng(APP_ROOT . self::WATERMARK_PATH);

$body_width     = (int) @imagesx($body);
$body_height    = (int) @imagesy($body);

$logo_width     = (int) @imagesx($logo);
$logo_height    = (int) @imagesy($logo);

$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);

imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);

imagejpeg($image, null, 85);

returns in: https://i.sstatic.net/nwtqZ.jpg

buuuuuuuuut...... if i add the body (the cat) to the image...

$body   = @imagecreatefromstring($image_data['body']);
$logo   = @imagecreatefrompng(APP_ROOT . self::WATERMARK_PATH);

$body_width     = (int) @imagesx($body);
$body_height    = (int) @imagesy($body);

$logo_width     = (int) @imagesx($logo);
$logo_height    = (int) @imagesy($logo);

$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);

imagecopy(
    $image, $body, 
    1, 1, 
    0, 0, $body_width, $body_height
);

imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);

imagejpeg($image, null, 85);

results in: https://i.sstatic.net/Xeb73.jpg

As you can see in this last one, the bottom of the image is corrupt or something...... wtf happened?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-11-04 10:55:27

尝试这段代码我发现绝对没有错误:

<?php
$body   = imagecreatefromjpeg('https://i.sstatic.net/KJjDi.jpg');
$logo   = imagecreatefromjpeg('http://b.vimeocdn.com/ps/161/161028_300.jpg');
$body_width     = (int) imagesx($body);
$body_height    = (int) imagesy($body);
$logo_width     = (int) imagesx($logo);
$logo_height    = (int) imagesy($logo);
$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);
imagecopy(
    $image, $body, 
    1, 1, 
    0, 0, $body_width, $body_height
);
imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);
header('Content-Type: image/jpeg');
imagejpeg($image, null, 85);
?>

我想说它与实际图像有关:-?

Trying this code I see absolutely no error:

<?php
$body   = imagecreatefromjpeg('https://i.sstatic.net/KJjDi.jpg');
$logo   = imagecreatefromjpeg('http://b.vimeocdn.com/ps/161/161028_300.jpg');
$body_width     = (int) imagesx($body);
$body_height    = (int) imagesy($body);
$logo_width     = (int) imagesx($logo);
$logo_height    = (int) imagesy($logo);
$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);
imagecopy(
    $image, $body, 
    1, 1, 
    0, 0, $body_width, $body_height
);
imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);
header('Content-Type: image/jpeg');
imagejpeg($image, null, 85);
?>

I'm gonna say it's got something to do with the actual images :-?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文