Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1842 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Saving a GD Image

Thread title: Saving a GD Image
Closed Thread    
    Thread tools Search this thread Display Modes  
12-16-2005, 10:58 PM
#1
Cooleo is offline Cooleo
Status: Member
Join date: Sep 2005
Location: Stoke, UK
Expertise:
Software:
 
Posts: 151
iTrader: 0 / 0%
 

Cooleo is on a distinguished road

Send a message via MSN to Cooleo

  Old  Saving a GD Image

Hey,
I have the following script to create a thumbnail by using the syntaxtn thumb.php?file=something.jpg&size=100

now, you cant link to that on forums, e.g. [img]thumb.php?file=something.jpeg&size=100[/img] wont work or will display a post error message

so, could the image generated be saved to a file, e.g. something_thumb.jpeg?

anyway, here's what i have so far
(btw ive tried changing the part with the md5(stings), but it display a 'this image cannot be shown because it contains errors' msg

<?php
//script configuration
$thumbnailfilename = 'thumb';
$thumb_size = 108; //all thumbnails are this maximum width or height if not specified via get
$site_config['absolute_uri']=str_replace('thumb.php?'.$_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']);
$site_config['path_thumbnail']=$site_config['absolute_uri'].'/1205/'; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOT
$no_image =$site_config['absolute_uri'].'/images/no_image.png'; // used if no image could be found
$filetype ='jpeg';

//define quality of image
if ($_GET["quality"]<>0) {
$quality = $_GET["quality"];
} else {
$quality = 80;
}

//define size of image (maximum width or height)- if specified via get.
if ($_GET["size"]<>0) {
$thumb_size=intval($_GET["size"]);
}
if (intval($_GET["sizex"])>0) {
$thumb_size_x=intval($_GET["sizex"]);
if (intval($_GET["sizey"])>0) {
$thumb_size_y=intval($_GET["sizey"]);
} else {
$thumb_size_y=$thumb_size_x;
}
}

//determine filesystem - *nix or windows - and set document_root. if the file is outside of the web server root path, the document_root is ignored and the absolute path of the image is used instead.
if (substr_count(strtolower($_ENV['OS']), "windows")>0)
{
//w32 filesystem
$site_config['document_root'] = str_replace(str_replace("/","\\\\",$_SERVER["SCRIPT_NAME"]),"",$_SERVER["PATH_TRANSLATED"]);
if (file_exists(str_replace("/","\\\\",$_GET['file'])))
{
$filename=str_replace("/","\\\\",$_GET['file']);
} else {
$filename=$site_config['document_root']."\\\\".str_replace("/","\\\\",$_GET['file']);
}

$site_config['document_root'] = str_replace($_SERVER["SCRIPT_NAME"],"",$_SERVER["PATH_TRANSLATED"]);
if (file_exists($_GET['file']))
{
$filename=$_GET['file'];
} else {
$filename=$site_config['document_root']."/".$_GET['file'];
}

} else {
//linux filesystem
$site_config['document_root'] = $_SERVER['DOCUMENT_ROOT'];
$checkfor=str_replace('//','/',$_GET["file"]);
$checkfor=rtrim($checkfor);
if (file_exists($checkfor))
{
$filename=$checkfor;
} else {
$filename=str_replace('//','/',$site_config['document_root'].$site_config['absolute_uri'].'/'.$_GET["file"]);
}
}

//if calling an external image, remove document_root
if (substr_count($filename, "http://")>0)
{
$filename=str_replace($site_config['document_root'].$site_config['absolute_uri'].'/','',$filename);
}

$filename=str_replace("\'","'",$filename);
$filename=rtrim($filename);
$filename=str_replace("//","/",$filename);

$no_image=$site_config['document_root'].$no_image;
$cache_file=$site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size.$thumb_size_x.$thumb_si ze_y.$quality).'.'.$filetype;

//if file exists in the cache and get variable nocache not true
if ($_GET['nocache']==1)
{
if (file_exists($cache_file))
{
//remove the cached thumbnail
unlink($cache_file);
}
}

// no caching on this server - turn off anyway
@unlink($cache_file);

if ((file_exists($cache_file)) && (@filemtime($cache_file)>@filemtime($filename)))
{
Header('Content-type: image/'.$filetype);
Header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
Header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thum b_size_y.$quality).'.'.$filetype));

echo (join('', file( $cache_file )));

exit; //no need to create thumbnail - it already exists in the cache
}

//determine php and gd versions
$ver=intval(str_replace(".","",phpversion()));
if ($ver>=430)
{
$gd_version=@gd_info();
}

//define the right function for the right image types
$image_type_arr=getimagesize($filename);
$image_type=$image_type_arr[2];

switch ($image_type)
{
case 1: // GIF
if (!$image = @ImageCreateFromGIF ($filename))
{
// not a valid png file
$image = ImageCreateFromPNG ($no_image);
$file_type="png";
if (file_exists($cache_file))
{
//remove the cached thumbnail
unlink($cache_file);
}
}
break;

case 2: // JPG
if (!$image = @ImageCreateFromJPEG ($filename))
{
// not a valid jpeg file
$image = ImageCreateFromPNG ($no_image);
$file_type="png";
if (file_exists($cache_file))
{
//remove the cached thumbnail
unlink($cache_file);
}

}
break;
case 3: // PNG
if (!$image = @ImageCreateFromPNG ($filename))
{
// not a valid png file
$image = ImageCreateFromPNG ($no_image);
$file_type="png";
if (file_exists($cache_file))
{
//remove the cached thumbnail
unlink($cache_file);
}
}
break;


case 1: // GIF - replace file with a 'no image' png
default:
$image = ImageCreateFromPNG ($no_image);
break;
case 2: // JPG
$image = ImageCreateFromJPEG ($filename);
break;
case 3: // PNG
$image = ImageCreateFromPNG ($filename);
break;
}

//define size of original image
$image_width = imagesx($image);
$image_height = imagesy($image);

//define size of the thumbnail
if ($thumb_size_x>0)
{
// define images x AND y
$thumb_width = $thumb_size_x; //$image_width/$resize_factor;
$factor = $image_width/$thumb_size_x;
$thumb_height = $image_height / $factor; //($image_width.$width_factor); //$resize_factor;
if ($thumb_height>$thumb_size_y)
{
$thumb_height = $thumb_size_y; //$image_width/$resize_factor;
$factor = $image_height/$thumb_size_y;
$thumb_width = $image_width / $factor; //($image_width.$width_factor); //$resize_factor;
}
} else {
// define images x OR y
$thumb_width = $thumb_size; //$image_width/$resize_factor;
$factor = $image_width/$thumb_size;
$thumb_height = $image_height / $factor; //($image_width.$width_factor); //$resize_factor;
if ($thumb_height>$thumb_size)
{
$thumb_height = $thumb_size; //$image_width/$resize_factor;
$factor = $image_height/$thumb_size;
$thumb_width = $image_width / $factor; //($image_width.$width_factor); //$resize_factor;
}
}

//create the thumbnail
if ($image_width<2000) //no point in resampling images larger than 2000 pixels wide - too much server processing overhead - a resize is more economical
{
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
//GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
//GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
} else {
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
//GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
//GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
}

// insert string
if ($_GET['tag']<>"")
{
$font=1;
$string= $_GET['tag'];
$white = imagecolorallocate ($thumbnail, 255, 255, 255);
$black = imagecolorallocate ($thumbnail, 0, 0, 0);
imagestring ($thumbnail, $font, 3, $thumb_height-9, $string, $black);
imagestring ($thumbnail, $font, 2, $thumb_height-10, $string, $white);
}

//create thumbnail in the cache because it doesn't exist yet.
Header('Content-type: image/jpeg');
Header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
Header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thum b_size_y.$quality).'.jpeg'));

if ($filetype=="jpeg")
{
imagejpeg($thumbnail,$cache_file,$quality);
imagejpeg($thumbnail,'',$quality);
} else {
imagepng($thumbnail,$cache_file);
imagepng($thumbnail);
}

//clear memory
imagedestroy ($image);
imagedestroy ($thumbnail);

?>

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed