将图像格式PNG转换为JPEG而不保存到磁盘-PHP [英] Convert image format PNG to JPEG without saving to disk - PHP

查看:131
本文介绍了将图像格式PNG转换为JPEG而不保存到磁盘-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我正在从以下网址获取PNG图片.
  • 我想将PNG图像转换为JPEG,而不用PHP保存磁盘.
  • 最后,我想将JPEG图像分配给$ content_jpg变量.

  • I am taking a PNG image from a url as below.
  • I want to convert the PNG image to JPEG without saving disk with PHP.
  • Finally I want to assign JPEG image to $content_jpg variable.

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

 $content_jpg=;

推荐答案

您要使用

You want to use the gd library for this. Here's an example which will take a png image and output a jpeg one. If the image is transparent, the transparency will be rendered as white instead.

<?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转换为JPEG而不保存到磁盘-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆