Solved

how JPG/JPEG to PNG and then PNG to JPG/JPEG

Posted on 2006-07-09
17
763 Views
Last Modified: 2011-10-03
I need to convert user uploaded JPG/JPEG image to PNG and then PNG to back to JPG/JPEG.

How can I convert from JPG/JPEG to PNG and then PNG to JPG/JPEG?
0
Comment
Question by:php_beginner
  • 9
  • 7
17 Comments
 
LVL 20

Expert Comment

by:Gawai
ID: 17071095
0
 

Author Comment

by:php_beginner
ID: 17071111
1. i dont want to use image magick.
2. i need to convert the image using PHP, not some online tool. Will it be possible to convert the user uploaded images one by one use Online Image Converter? there will hundreds of images uploaded daily.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17071612
When your server supports it (most likely it does when it is hosted provider) you can use:

<?php
$im = createfromjpeg('myimage.jpg');
imagecreatepng($im, 'myimage.png');
?>
And
<?php
$im = createfrompng('myimage.jpg');
imagecreatejpeg($im, 'myimage.png');
?>

-r-
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:php_beginner
ID: 17071839
I am unable to find these functions in PHP.
0
 

Author Comment

by:php_beginner
ID: 17072189
i have found a solution to convert PNG and then back to JPG. But there is an error on this line
$im = imagecreatefrompng($png_image);

The error is

<br />
<b>Warning</b>:  imagecreatefrompng(‰PNG

): failed to open stream: Invalid argument in <b>d:\apache\htdocs\test\gd.php</b> on line <b>22</b><br />
<br />
<b>Warning</b>:  imagejpeg(): supplied argument is not a valid Image resource in <b>d:\apache\htdocs\test\gd.php</b> on line <b>23</b><br />

Is it required to pass a physical image file resource?

<?
$image_source = "audio.jpg";

/********* PNG to JPG **********************/
ob_start();
header ("Content-type: image/png");
$im = imagecreatefromjpeg($image_source);
imagepng($im);
$png_image = ob_get_contents();
ob_end_clean();
/*******************************************/

/********* JPG to PNG **********************/
ob_start();
$quality = 99;
header ("Content-type: image/jpeg");
$im = imagecreatefrompng($png_image);
imagejpeg($im, '', $quality);
$jpg_image = ob_get_contents();
ob_end_clean();
/*******************************************/

?>

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17072255
Yes, my fault. createfromjpeg/createfrompng had to be imagecreatefromjpeg/imagecreatefrompng.

The error you have in PNG to JPEG is because you haven't set a valid filename for the $image_source variable.

-r-
0
 

Author Comment

by:php_beginner
ID: 17072336
>>The error you have in PNG to JPEG is because you haven't set a valid filename for the $image_source variable.

I didnt understand. Can you please correct that error?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17072347
You have to add a line:
$image_source = 'myimage.jpg'

Make sure that myimage.jpg is an actual image.

-r-
0
 

Author Comment

by:php_beginner
ID: 17072486
I have this line on the top of the code. PNG image is creating correctly, when i echo $png_image it displays the image in the browser.

But giving error on $im = imagecreatefrompng($png_image);

<b>Warning</b>:  imagecreatefrompng(‰PNG

): failed to open stream: Invalid argument in <b>d:\apache\htdocs\test\gd.php</b> on line <b>22</b><br />
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17072503
Please show full code. "PNG image is creating correctly" is somewhat obscure. $png_image just needs to be a filename/path to filename, nothing more, nothing less.

-r-
0
 

Author Comment

by:php_beginner
ID: 17072542
I think you didnt read my posts carefully.

>>$png_image just needs to be a filename/path to filename, nothing more, nothing less.

In my previous post i asked "Is it required to pass a physical image file resource?".

This is the full source code and posting it 2nd time.

<?
$image_source = "audio.jpg";

/********* PNG to JPG **********************/
ob_start();
header ("Content-type: image/png");
$im = imagecreatefromjpeg($image_source);
imagepng($im);
$png_image = ob_get_contents();
ob_end_clean();
/*******************************************/

/********* JPG to PNG **********************/
ob_start();
$quality = 99;
header ("Content-type: image/jpeg");
$im = imagecreatefrompng($png_image);
imagejpeg($im, '', $quality);
$jpg_image = ob_get_contents();
ob_end_clean();
/*******************************************/

?>
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 130 total points
ID: 17072580
You cannot use it like this. You indeed need to pass a physical string (as mentioned in the manual).
I am sorry for not reading correctly.

You can output to file using:
imagepng($im, 'image.png');

Then use $png_image = 'image.png';

-r-
0
 

Author Comment

by:php_beginner
ID: 17072670
There should be an option to award points to myself :-)
0
 

Author Comment

by:php_beginner
ID: 17072679
It means i need to save the image on disk and then use it. It will increase the IO operations:-s
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17072698
You can also just keep using the $im variable, as that is the memory representation of an image, not specifically a JPG-image. Your above code can also be:

<?
$image_source = "audio.jpg";
ob_start();
header ("Content-type: image/png");
$im = imagecreatefromjpeg($image_source);
$quality = 99;
header ("Content-type: image/jpeg");
imagejpeg($im, '', $quality);
$jpg_image = ob_get_contents();
ob_end_clean();
/*******************************************/

?>

-r-
0
 

Author Comment

by:php_beginner
ID: 17072757
In your code, i cant see you creating png image?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17072777
You are correct. Thought you where doing that just for the fun of it.
You can create png and jpg from same $im variable:
<?
$image_source = "audio.jpg";
ob_start();
header ("Content-type: image/png");
$im = imagecreatefromjpeg($image_source);
imagepng($im, 'audio.png');
$quality = 99;
header ("Content-type: image/jpeg");
imagejpeg($im, '', $quality);
$jpg_image = ob_get_contents();
ob_end_clean();
/*******************************************/

?>

-r-
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question