Convert JPEG to a Progressive JPEG in PHP

若如初见. 提交于 2019-12-09 01:45:41

问题


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

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);

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.

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.


回答1:


Create image resource with imagecreatefromstring:

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



回答2:


Try Imagick (ImageMagic Package) as shown in here : http://php.net/manual/en/imagick.setinterlacescheme.php

$image = new Imagick('image.jpg');

$image->thumbnailImage(500, 0);

$image->setInterlaceScheme(Imagick::INTERLACE_PLANE);

$image->writeImage('progressive.jpg');



回答3:


Use imageinterlace.

$src_img = imagecreatefromjpeg('source.jpg');

imageinterlace($src_img, true);

imagejpeg($src_img, 'destination.jpg');

imagedestroy($src_img);


来源:https://stackoverflow.com/questions/41732652/convert-jpeg-to-a-progressive-jpeg-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!