[LONG POST] GD wont display the image [COMMENTED]
Posted: Thu Jun 09, 2005 1:18 am
basically, the picture just wont output, i get placeholder with an X, any ideas?
ps. its my first effort at a class so dont be too harsh
, some things arent implemented yet even though the functions are written, but the output should be working
the call
usage
any help would be much appreciated
as a side note, ive tried commenting the line..
thinking maybe that was the error, alas, no
also firefox says
i understand this is a long post, but in suggestions would be much appreciated, its all a learning experience for me atm with classes..
also if any of you guys could give me some tips on the structure of my class (not my coding of the functions) that would be very appreicated also as i feel it is better to learn the standard than do things wrong from the beginning
ps. its my first effort at a class so dont be too harsh
Code: Select all
<?php
// image class
class Ezthumb
{
var $fpath;
///////////////////////////////////////////////////
// CONSTRUCTOR //
///////////////////////////////////////////////////
/**
* @return void
* @param $fpath
* @desc EzThumb class: Automatically generate thumbnails on the fly, as well as thumbnail file output
*/
function EzThumb ($fpath)
{
$this->image->filepath = $fpath; // set a nice fpath value
// unset filepath to make it look nicer
unset($this->fpath); // unset it
$this->CheckGDSupport ();
$this->CheckUserRequirements ();
$this->CheckImageAvailable ();
$this->GetImageInformation(); // get width. height and type of image
$this->GetImageFilesize(); // get the filesize of the image
$this->FormatImageFilesize(); // format it
$this->GetImageExtension (); // do an explode to get the extension on the filename
$this->GetImageType (); // check type as defined from getimagesize
$this->CheckWallpaper (); // check to see if it has the dimensions of a wallpaper image
$this->ThumbScale (); // get the ratio to scale to
$this->ShowImageThumbnail (); // actually generate the thumbnail (writing of thumbnail also designated here)
}
//
/**
* @return void
* @desc Converts and checks the users settings and passes them to the object
*/
function CheckUserRequirements ()
{
// first check the expected vars are of the type required
if (!is_numeric($_GET['max_height']) && ($_GET['max_width']))
{
// we're expecting numberical values, they arent numberic
print "Expecting Numeric value for max height + max width";
exit;
}
if (isset($_GET['max_height']) || ($_GET['max_width']))
{
// one or the other is set
// its set
$this->userreq->maxheight = $_GET['max_height'];
}
else
{
// its not set, set a default
$this->userreq->maxheight = 80;
}
if (isset($_GET['max_height']))
{
// its set
$this->userreq->maxwidth = $_GET['max_width'];
}
else
{
// its not set, set a default
$this->userreq->maxwidth = 80;
}
}
//
/**
* @return void
* @desc Checks the filesystem/url to see if the file exists
*/
function CheckImageAvailable ()
{
if (!is_file($this->image->filepath))
{
print "Filepath is invalid for <strong>{$this->image->filepath}</strong>";
exit;
}
}
//
/**
* @return void
* @desc Generates various information about an image such as bits per channel and mime type
*/
function GetImageInformation ()
{
// get an array back with information that i can decide what to put into the object
$array = getimagesize($this->image->filepath);
// assign values i want to store
$this->image->width = $array[0];
$this->image->height = $array[1];
$this->image->type = $array[2];
$this->image->bits = $array['bits'];
$this->image->channels = $array['channels'];
$this->image->mime = $array['mime'];
}
//
/**
* @return void
* @desc Simple function that parses the filename to retrieve the extension
*/
function GetImageExtension ()
{
$explode = explode(".", $this->image->filepath);
$this->image->extension = array_pop($explode);
}
//
/**
* @return void
* @desc Handles the actual output of the thumbnail, also calls the thumbnail writing process if required
*/
function ShowImageThumbnail ()
{
// check to see if the image needs resizing before output
if ($this->imagescale < 1)
{
// it does Scaling down required
// output headers
#header ("Content-type: {$this->image->mime}");
$this->thumbnail->width = ($this->image->scale * $this->image->width);
$this->thumbnail->height = ($this->image->scale * $this->image->width);
$temp = imagecreate($this->thumbnail->width, $this->thumbnail->height) or die ("Cannot Create Thumbnail: Possible Problem With Function Coding");
$temp = imagecopyresized($temp, $this->image->filepath, 0, 0, 0, 0, $this->thumbnail->width, $this->thumbnail->height, $this->image->width, $this->image->height);
$thumbnail = $temp;
imagejpeg($thumbnail);
imagedestroy($temp);
}
else
{
// it doesnt
}
}
//
/**
* @return void
* @desc Handles the writing of the thumbnail to the file system
*/
function WriteThumbnail ()
{
//
}
//
/**
* @return void
* @desc function that converts the code return by getimagesize into the actual file type
*/
function GetImageType ()
{
// define the array of definitions
$type_definitions = array(1 => "GIF", 2 => "JPG", 3 => "PNG", 4 => "SWF",5 => "PSD",
6 => "BMP", 7 => "TIFF(intel byte order)", 8 => "TIFF(motorola byte order)",
9 => "JPC", 10 => "JP2", 11 => "JPX", 12 => "JB2", 13 => "SWC", 14 => "IFF", 15 => "WBMP", 16 => "XBM");
$this->image->type = $type_definitions[$this->image->type];
}
//
/**
* @return void
* @desc Another Simple function that gets the filesize of the passed filename/filepath
*/
function GetImageFilesize ()
{
$this->image->filesize = filesize($this->image->filepath);
// we'll now check if its been set, if it has'nt that means its a remote file
// and we need to change the way we check it
if (empty($this->image->filesize))
{
// its empty
// reset the value
print "it was initially empty";
}
}
//
/**
* @return void
* @desc nice feature that converts the byte value given by PHP into KB + MB
*/
function FormatImageFilesize ()
{
$imgsize = filesize($this->image->filepath) / 1024;
$imgsize = round($imgsize, 0);
// KB is set, but now we'll check if its higher than 1000 and put it in as MB
if ($imgsize >= 1000)
{
$this->image->formatfilesize = ($imgsize / 1000);
$this->image->formatfilesizeunit = "MB";
}
else
{
$this->image->formatfilesize = $imgsize;
$this->image->formatfilesizeunit = "Kb";
}
}
//
/**
* @return void
* @desc Check to see if the image has the same resolution as wallpaper
*/
function CheckWallpaper ()
{
$array_width = array("640", "800", "1024", "1152", "1280", "1280", "1600");
$array_height = array("480", "600", "768", "864", "960", "1024", "1200");
if (in_array($this->image->height, $array_height) && in_array($this->image->width, $array_width))
{
// is a wallpaper
$this->image->wallpaper = 1;
}
else
{
$this->image->wallpaper = 0;
}
}
//
/**
* @return void
* @desc Automatically calculates the ratio to resize the thumbnail to
*/
function ThumbScale ()
{
$this->image->scale = min($this->userreq->maxwidth / $this->image->width, $this->userreq->maxheight / $this->image->height);
}
//
/**
* @return void
* @desc Check to see if GD is supported on the users server,also returns GD version information
*/
function CheckGDSupport ()
{
// precedence of use should go to GD version 2 if available
// just continue to set it as GD2 is eval'd last it will overwrite it
// check original GD
if (extension_loaded("gd"))
{
$this->env->gdavailable = 1;
// get version of GD
$array = gd_info();
$this->env->gdversion = $array['GD Version'];
}
else
{
// no GD extension has been loaded
print "No GD Library available";
exit;
}
}
//
}Code: Select all
if ($_GET['call'] == "y")
{
$call = new EzThumb($_GET['filename']); // filepath relative to document
}Code: Select all
http://127.0.0.1/test/index.php?call=y&filename=picture.jpgas a side note, ive tried commenting the line..
Code: Select all
header ("Content-type: {$this->image->mime}");also firefox says
also thought i would add a print out of the actual objectfirefox wrote: The image "http://path/to/image" cannot be displayed, because it contains errors.
Code: Select all
ezthumb Object
(
їimage] => stdClass Object
(
їfilepath] => pureskillz.jpg
їwidth] => 200
їheight] => 200
їtype] => JPG
їbits] => 8
їchannels] => 3
їmime] => image/jpeg
їfilesize] => 19359
їformatfilesize] => 19
їformatfilesizeunit] => Kb
їextension] => jpg
їwallpaper] => 0
їscale] => 0.4
)
їenv] => stdClass Object
(
їgdavailable] => 1
їgdversion] => bundled (2.0.28 compatible)
)
їuserreq] => stdClass Object
(
їmaxheight] => 80
їmaxwidth] => 80
)
)also if any of you guys could give me some tips on the structure of my class (not my coding of the functions) that would be very appreicated also as i feel it is better to learn the standard than do things wrong from the beginning