Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert .jpg / .gif / .bmp to .png in php

Status
Not open for further replies.

C0FFEE123

Programmer
Sep 1, 2008
44
NL
Is it possible to convert a .jpg / .gif / .bmp to .png in PHP.

I tried to look for it everywhere but i couldn't find it.

Thanks
 
Hi

Yes, it is possible. Usually with the GD library, but there are others too.

There may be a simpler way, but I am used with this :
Code:
imagepng(imagecreatefromgif("filename.gif"),"filename.png");

Feherke.
 
Well exualy i am sending a .jpg or .gif or .bmp to a page.
That page has to convert it to a .png and resize it and put it in a folder.
Working on the resize now but how to convert it to a .png i realy have no idea how to do that.
I also know that it is possible to do with .gif but some people are saying that it isn't possible for .jpg.
 
Could someone please give me example?
For example:

I upload a .jpg with a form then i upload the file to a map.
In between i want to convert the .jpg to .png without a background.
 
feherke has already given you an example. what more do you want?
 
That is only for .gif files.
I want it for .jpg files

I am a progammer.... no realy.
 
It's the same example that feherke gave, just replace 'imagecreatefromgif' with 'imagecreatefromjpeg'

|| ABC
 
Yeah i did that...
Here is the code:

Code:
<?php

//echo $_POST;

	include_once '../includes/db.inc.php';
	include_once 'admin.inc.php';
check_login();
top_admin('occasions', $pagina_titel);	
open_db();
$sql="INSERT INTO occasion (occasion_name, occasion_short_description, occasion_long_description, occasion_price, occasion_picture, occasion_picture_medium)
VALUES
('$_POST[artikel]', '$_POST[korte_omschrijving]', '$_POST[uitgebreide_omschrijving]', '$_POST[prijs]', '" . $_FILES[plaatje][name] . "', '" .$_FILES[plaatje_m][name]. "')";
//echo $sql . "<br />" . "<br />";
mysql_query($sql);

$uploadedfile = $_FILES['plaatje']['tmp_name'];
$uploadedfile2 = $_FILES['plaatje_m']['tmp_name'];

$src = imagecreatefromjpeg($uploadedfile);
$src2 = imagecreatefromjpeg($uploadedfile2);

list($width,$height)=getimagesize($uploadedfile);
list($width2,$height2)=getimagesize($uploadedfile2);

$newwidth=60;
$newheight=80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth2=300;
$newheight2=300;
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp2,$src2,0,0,0,0,$newwidth2,$newheight2,$width2,$height2);


$filename = "../occasions/fotos_small/". $_FILES['plaatje']['name'];
imagepng($tmp,$filename,9);
$filename2 = "../occasions/fotos_medium/". $_FILES['plaatje_m']['name'];
imagepng($tmp2,$filename2,9);

imagedestroy($src);
imagedestroy($tmp); 

imagedestroy($src2);
imagedestroy($tmp2); 
?>

-------------------------------------
I am a progammer.... no realy.
 
Yay i got it and it works ! :)
I did it like this now:
Code:
original_filename = "../occasions/fotos_small/".$_FILES['plaatje']['name'];
$new_filename = trim( strtolower( $original_filename ), 'jpg' ) . 'png'; 

$original_filename2 = "../occasions/fotos_medium/".$_FILES['plaatje_m']['name'];
$new_filename2 = trim( strtolower( $original_filename2 ), 'jpg' ) . 'png'; 

//echo $sql . "<br />" . "<br />";
mysql_query($sql);

//$filename = "../occasions/fotos_small/". $_FILES[plaatje][name];
imagepng($tmp,$new_filename,9);
//$filename2 = "../occasions/fotos_medium/". $_FILES[plaatje_m][name];
imagepng($tmp2,$new_filename2,9);

-------------------------------------
I am a progammer.... no realy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top