Jump to content

[SOLVED] Convert Image and Save to disk


PhaZZed

Recommended Posts

Greetings,

 

I am writing a small script to convert from .jpg to .gif

 

$filename = $_GET['file']; // always .jpg
$type = $_GET['type'];

$im = @imagecreatefromjpeg($filename);
if ($im === false)
{
die('Unable to open image');
}
if ($type == "gif")
{		
header ('Content-Type: image/gif');
        imagegif($im);
        imagedestroy($im);
}

 

This script displays the image on the browser page, whereas I would like to actually have a variable pointing to the relative path, ie. $location = "http://www.xx.com/uploads/image.gif"

 

Any suggestions? I read that imagegif($im,$destination); can be used, but it's giving me errors..

Link to comment
https://forums.phpfreaks.com/topic/64812-solved-convert-image-and-save-to-disk/
Share on other sites

Solution..

 

$filename = $_GET['file'];
$type = $_GET['type'];

$im = @imagecreatefromjpeg($filename);
if ($im === false)
{
die('Unable to open image');
}

srand((double)microtime()*1000000); 
$rand = rand(999999,99999999);

$dest = "../temps/" . $rand;

if ($type == "gif")
{		
	$dest .= ".gif";
	imagegif($im,$dest);
        imagedestroy($im);
        $filename = $dest;
}
if ($type == "png")
{
	$dest .= ".png";
	imagepng($im,$dest);
        imagedestroy($im);
        $filename = $dest;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.