iakiak
iakiak

Reputation: 109

PHP: how can I convert jpeg to png and then zip (without making a copy)

So what I'm trying to do is: - given an image url -> convert image to png - zip resulting png

I have the following code which successfully does the conversion and zipping (I'm going to expand it later to test the extension to auto convert formats):

$file = "../assets/test.jpg";
$img = imagecreatefromjpeg($file);
imagePng($img, "files/temp.png" );
$zip->addFile( "files/temp.png", "test.png" );

What I want to know is, is it possible to do this without creating a copy of image before it's zipped

Upvotes: 1

Views: 1100

Answers (1)

nice ass
nice ass

Reputation: 16709

See ZipArchive::addFromString().

$file = "../assets/test.jpg";

// capture output into the internal buffer
ob_start();

$img = imagecreatefromjpeg($file);
imagepng($img);

// get contents from the buffer
$contents = ob_get_clean();

$zip = new ZipArchive();
$zip->open('archive.zip', ZipArchive::CREATE);

// and put them in the zip file...
$zip->addFromString('name_in_the_zip.png', $contents);

Upvotes: 3

Related Questions