Solved

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

Posted on 2006-07-09
17
759 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP/Magento not logging 2 36
calculated column 12 74
Apache/PHP: Cannot make phpinfo() and phpmyadmin work 4 37
Configuring a checkbox in CSS and php 18 32
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

895 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

12 Experts available now in Live!

Get 1:1 Help Now