Solved

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

Posted on 2006-07-09
17
757 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
Comment Utility
0
 

Author Comment

by:php_beginner
Comment Utility
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
Comment Utility
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
 

Author Comment

by:php_beginner
Comment Utility
I am unable to find these functions in PHP.
0
 

Author Comment

by:php_beginner
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
You have to add a line:
$image_source = 'myimage.jpg'

Make sure that myimage.jpg is an actual image.

-r-
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:php_beginner
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
There should be an option to award points to myself :-)
0
 

Author Comment

by:php_beginner
Comment Utility
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
Comment Utility
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
Comment Utility
In your code, i cant see you creating png image?
0
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now