Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像格式PNG转换为JPEG而不保存到磁盘-PHP 我从下面的url获取PNG图像 我想把PNG图像转换成JPEG,而不用用PHP保存磁盘_Php_Gdlib - Fatal编程技术网

将图像格式PNG转换为JPEG而不保存到磁盘-PHP 我从下面的url获取PNG图像 我想把PNG图像转换成JPEG,而不用用PHP保存磁盘

将图像格式PNG转换为JPEG而不保存到磁盘-PHP 我从下面的url获取PNG图像 我想把PNG图像转换成JPEG,而不用用PHP保存磁盘,php,gdlib,Php,Gdlib,最后,我想将JPEG图像分配给$content\u jpg变量 $url = 'http://www.example.com/image.png'; $content_png = file_get_contents($url); $content_jpg=; 您希望为此使用。下面是一个示例,它将获取一个png图像并输出一个jpeg图像。如果图像是透明的,则透明度将被渲染为白色 <?php $file = "myimage.png"; $image = imagecreate

最后,我想将JPEG图像分配给$content\u jpg变量

 $url = 'http://www.example.com/image.png';
 $content_png = file_get_contents($url);

 $content_jpg=;
您希望为此使用。下面是一个示例,它将获取一个png图像并输出一个jpeg图像。如果图像是透明的,则透明度将被渲染为白色

<?php

$file = "myimage.png";

$image = imagecreatefrompng($file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));

imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);

header('Content-Type: image/jpeg');

$quality = 50;
imagejpeg($bg);
imagedestroy($bg);

?>

简单的答案是

// PNG image url
$url = 'http://www.example.com/image.png';

// Create image from web image url
$image = imagecreatefrompng($url);

// Start output buffer
ob_start(); 

// Convert image
imagejpeg($image, NULL,100);
imagedestroy($image);

// Assign JPEG image content from output buffer
$content_jpg = ob_get_clean();

“转换图像”是什么意思?如果要编辑图像,不需要保存图像,只需输出即可。看:我在Oxwall做这个。转换后,我将保存jpg图像到系统生成的位置。但我想知道在不写入磁盘的情况下将png图像内容($content_png)转换为jpg的可能性。这是我找到的一个教程中的C#实现的。@Josh,我想将图像格式从png更改为JPEG。这就是我想说的“转换图像”而不是“编辑图像”。Thanks@ShankarDamodaran,我会将图像输出到浏览器。如果您有比我们发现的更好的解决方案,请告诉我们。谢谢。imagecreatefrompng函数也可以接受URL。所以我将url传递给函数,而不是一个文件。此代码按预期工作,并将JPEG文件输出到浏览器。非常感谢你,乔希。