【问题标题】:Convert any type of an image to PNG and add to PNG将任何类型的图像转换为 PNG 并添加到 PNG
【发布时间】:2016-09-04 09:17:57
【问题描述】:

我正在尝试为我开发的网站创建员工海报。我的目标是从服务器上任何文件类型(.png、.gif、.jpeg 等)的目录中获取图像并将其复制到另一个生成的图像上,然后将其输出到浏览器。

问题是我使用:

$final_image = imagecreatefrompng("large_background.png");

用于制作最终图像,出于某种原因,如果我添加类型为 jpeg、gif 等的个人资料图像(任何不是 jpeg 的类型),它就不起作用。图像永远不会出现在输出中。但是,如果我使用 png,它确实可以工作。

为了解决这个问题,我尝试将图像转换为 png,然后从中创建一个 png,如下面的代码所示。不幸的是,它不起作用。个人资料图片仍然没有显示在背景上。

// get image from database 
$image_from_database = could be a .png, .jpeg, .gif, etc.

// get the image from the profile images directory
$path = "profile_images/".$image_from_database;

// create a png out of the image
$image = imagecreatefrompng(imagepng($path));

// add the $image to my larger $final_image (which is a png)
imagecopy($final_image, $image, $x, $y, 0,0, $height, $width);
imagepng($final_image, $ouput_url);
...

谁能告诉我为什么这不起作用?我的个人资料图片未显示在最终图片的输出中。

我的问题,

  • 这条线imagecreatefrompng(imagepng(...)); 甚至可能吗?本质上,我想将任何类型的图像转换为 png,然后从中创建一个 png。

【问题讨论】:

  • 正如我所说,如果我这样做,当我将 imagecopy 放到 png 上时它不会出现。
  • 您是否阅读过 imagepng 期望的参数以及返回的内容?或者它实际上在做什么?
  • 所以你提议:$image = imagecreatefrompng(imagepng(imagecreatefromjpeg($path)));
  • $image = imagecreatefrompng($path); 用于 png 图像,$image = imagecreatefromjpeg($path); 用于 jpeg 图像,$image = imagecreatefromgif($path); 用于 gif 图像。

标签: php image image-processing image-manipulation


【解决方案1】:

使用imagecreatefrom* 函数读取图像后,原始格式是什么都没有关系。
imagecreatefrom* 函数返回图像资源。加载图像时,您使用的是图像的内部表示,而不是 PNG、JPEG 或 GIF 图像。
如果图片加载成功imagecopy应该没有问题。

此代码使用不同格式的图像并且可以正常工作:

$img = imagecreatefrompng('bg.png');
$png_img = imagecreatefrompng('img.png');
$jpeg_img = imagecreatefromjpeg('img.jpeg');
$gif_img = imagecreatefromgif('img.gif');

/* or use this, so you don't need to figure out which imagecreatefrom* function to use
$img = imagecreatefromstring(file_get_contents('bg.png'));
$png_img = imagecreatefromstring(file_get_contents('img.png'));
$jpeg_img = imagecreatefromstring(file_get_contents('img.jpeg'));
$gif_img = imagecreatefromstring(file_get_contents('img.gif'));
*/
imagecopyresampled($img, $png_img,   10, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $jpeg_img, 120, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $gif_img,  230, 10, 0,0, 100, 100, 200, 200);

header('Content-Type: image/png');
imagepng($img);

你的例子

$image = imagecreatefrompng(imagepng($path));

错了。
imagepng用于将图片资源输出为PNG图片。如果您提供路径作为第二个参数,则会创建 PNG 图像文件,否则会像 echo 那样打印到输出。
imagepng 实际返回的是一个布尔值,表示是否输出成功。
然后,您将该布尔值传递给 imagecreatefrompng,它需要一个文件路径。这显然是错误的。

我怀疑您在加载图像时遇到问题。
imagecreatefrom* 函数在失败时返回 FALSE,如果您有任何问题,您应该检查一下。

也许您的图像路径是相对于文档根目录的,而您的工作目录不同。
或者你有权限问题。
或者您的图片丢失了。
从你的问题无法判断。

【讨论】:

    【解决方案2】:

    我只是在运行一些本地测试...以下工作:

    $src = imagecreatefromgif('test.gif');
    $dest = imagecreatefrompng('test.png');
    
    imagecopy($dest, $src, 0, 0, 0, 0, 100, 100);
    
    header('Content-Type: image/png');
    imagepng($dest);
    
    imagedestroy($src);
    imagedestroy($dest);
    

    也是这样:

    $src = imagecreatefromstring(file_get_contents('test.gif'));
    

    如果您在尝试后一个示例后仍然遇到问题,请更新您的问题。除了功能代码示例之外,您使用的实际图像会很有帮助。

    【讨论】:

    • 你能用jpeg试试吗?我发现这在使用 jpeg 时不起作用。
    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 2012-06-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2011-02-01
    相关资源
    最近更新 更多