Solved

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

Posted on 2006-07-09
17
762 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cpanel file manager 8 41
Custom Wordpress Loop 22 39
Displaying text in text field when clicking on ajax search result 10 39
Link failure 16 34
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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 dynamically set the form action using jQuery.

828 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