- 我正在从以下网址获取 PNG 图像。
- 我想在不使用 PHP 保存磁盘的情况下将 PNG 图像转换为 JPEG。
最后,我想将 JPEG 图像分配给 $content_jpg 变量。
$url = 'http://www.example.com/image.png'; $content_png = file_get_contents($url); $content_jpg=;
问问题
6860 次
2 回答
5
简单的答案是,
// 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();
于 2014-01-15T18:28:57.263 回答
4
您想为此使用gd 库。这是一个示例,它将采用 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);
?>
于 2014-01-14T17:36:11.083 回答