在PHP中将JPEG转换为渐进JPEG [英] Convert JPEG to a Progressive JPEG in PHP

查看:112
本文介绍了在PHP中将JPEG转换为渐进JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将已经创建的JPEG重新保存为 progressive jpg.

I'm trying to re-save an already created JPEG as a progressive jpg.

基本上,在前端,我有一些JS可以裁剪图像,并输出base64图像JPEG.将其传递到PHP脚本后,我将其创建为常规JPEG文件,如下所示:

Basically, on the front end I have some JS that crops an image, and outputs a base64 image JPEG. Once it is passed to the PHP script, I create it as a regular JPEG file, like so:

$largeTEMP = explode(',', $_POST['large']);
$large = base64_decode($largeTEMP[1]);
file_put_contents('../../images/shows/'.$dirName.'/large.jpg', $large);

我希望这个jpg图像是渐进式的,所以我环顾四周,发现了PHP函数imageinterlace;但是,我似乎无法使其正常工作.我已经尝试了各种组合,但是由于某种原因,我觉得我以错误的方式进行操作.

I'm wanting this jpg image to be progressive, so I was looking around and found the PHP function imageinterlace; however, I can't seem to get it to work. I've tried various combinations, but I feel like I'm going about this in the wrong way for some reason.

所以我的问题是,如何使用PHP生成已经生成的JPEG并将其转换为渐进式?或者,更好的是,在我什至没有保存它之前,先将其转换为渐进式JPEG.

So my question is, how can I take my already generated JPEG and convert it to be progressive using PHP? Or, better yet, convert it to a progressive JPEG before I even save it in the first place.

推荐答案

使用创建图像资源imagecreatefromstring :

$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im === false) {
  die("imagecreatefromstring failed");
}
imageinterlace($im, true);
imagejpeg($im, 'new.jpg');
imagedestroy($im);

这篇关于在PHP中将JPEG转换为渐进JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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