Understanding Noah's Classifieds/Image.php Source
Appearance
<?php
// GLOBALS
// Setup our image creation/sampling tools
$g_Createfg = array("", "ImageCreateFromGIF", "ImageCreateFromJPEG", "ImageCreateFromPNG");
$g_Savefg = array("", "ImageGIF", "ImageJPEG", "ImagePNG");
$g_Extensions = array("", "gif", "jpg", "png");
$g_CheckBits = array(0, IMG_GIF, IMG_JPG, IMG_PNG);
function RatioImageSize($imageSize,$width,$height,$upscale = false)
{
if (($imageSize[0] > $width || $imageSize[1] > $height) || $upscale)
{
$ratio = $imageSize[0]/$width;
if( $imageSize[1]/$ratio > $height) $ratio=$imageSize[1]/$height;
// Now copy over the new size
$imageSize[0] = $imageSize[0]/$ratio;
$imageSize[1] = $imageSize[1]/$ratio;
}
return $imageSize;
};
function array_to_str ($arr_input, $level=1){
$str_return = "array(\n";
if ( is_array($arr_input) === false ){
return false;
}
foreach ( $arr_input as $key => $value ){
for ( $i=0; $i<$level; $i++ ){
$str_return .= "\t";
}
$str_return .= "[$key] => ";
if ( is_array($value) ){
$str_return .= array_to_str ($value, $level+1);
} else {
$str_return .= "$value\n";
}
}
for ( $i=0; $i<($level-1); $i++ ){
$str_return .= "\t";
}
return $str_return.")\n";
};
class myImage
{
var $size;
var $type;
var $imageData;
var $imageName;
var $supported; // Re-sizing supported?
// Reads the actual image from disk, returns false if failure
function ReadImage($imageName)
{
global $g_Createfg,$g_CheckBits;
$this->imageName = $imageName;
// Get the image's current size and file type
$this->size = getimagesize($this->imageName);
$this->type = $this->size[2];
$this->imageData = "";
// Check if image resizing is supported and handling is supported by this php build for this specific extension
if( defined("IMG_GIF") && function_exists("ImageTypes"))
{
$this->supported = isset($g_CheckBits[$this->type]) && ((ImageTypes() & $g_CheckBits[$this->type]));
if ($this->supported)
{
// Read the image from file
$this->imageData = $g_Createfg[$this->type]($this->imageName);
if (!$this->imageData)
{ // Image Creation Failed!!
// Perhaps handle and error msg here if you want
$this->supported = false;
}
}
}
else
{
$this->$supported = false;
$this->imageData = false;
}
return $this->imageData;
}
// Creates an image on the local host with the new filename, remembering that the image is currently in a temporary folder
function CreateLocal($targetName,$targetWidth,$targetHeight,$upscale=false)
{
// Include required globals
global $g_Createfg,$g_Savefg,$g_Extensions;
if (($this->size[0] < $targetWidth) && ($this->size[1] < $targetHeight))
{
// If no upscalling needed just copy the image
if ($upscale == false)
return copy($this->imageName,$targetName);
// Upscale the image nonetheless
}
// Get the new size
$newSize = RatioImageSize($this->size,$targetWidth,$targetHeight,$upscale);
// Does the newSize differ at all form the old size?
if (($this->size[0] == $newSize[0]) && ($this->size[1] == $newSize[1]))
return copy($this->imageName,$targetName);
// Image can't be resized, not supported
if (!$this->supported) return false;
// Create the memory required for a true color image of size newSize
$dst_im = ImageCreateTrueColor ($newSize[0], $newSize[1]);
// Copy over the image data resampling
imagecopyresampled ($dst_im, $this->imageData, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
// Save the new image
return $g_Savefg[$this->type]( $dst_im, $targetName);
}
}
?>