Solved

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

Posted on 2006-07-09
17
768 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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