Smart Image Processor PHP Support Product Page
This topic was archived
Code update to add prefix
Shared 02 Jun 2003 19:45:28
1
likes this idea
02 Jun 2003 19:45:28 Daniel Hovis posted:
I have added code to the incResize.php file to add a prefix (curriently hard coded to thmb_) rather than a sufix. The dialog box suffix field is no longer used. It would have been nicer to hve the option... ======================================================
<?php
// --- Smart Image Processor ----------------------------------------------------
// Copyright 2003 (c) George Petrov, Patrick Woldberg, www.DMXzone.com
//
// Version: 1.0.2
// prefix added by Daniel Hoviss
// 06/02/03
// -----------------------------------------
class resizeUploadedFiles extends pureUploadAddon
{
var $component; //GD of NetPBM?
var $resizeImages;
var $maxWidth;
var $maxHeight;
var $quality;
var $makeThumb;
var $suffix;
var $prefix = thmb_;
var $maxWidthThumb;
var $maxHeightThumb;
var $qualityThumb;
var $redirectURL;
var $newWidth;
var $newHeight;
function resizeUploadedFiles(&$upload) {
parent:<img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle>ureUploadAddon($upload);
$this->upload->registerAddOn($this);
}
function doResize() {
switch ($this->component) {
case 'GD':
$this->resize_GD();
break;
case 'NetPBM':
$this->resize_NetPBM();
break;
}
}
function calculateSize($imgWidth, $imgHeight, $create) {
if ($create == "image"
$maxWidth = $this->maxWidth;
$maxHeight = $this->maxHeight;
} else {
$maxWidth = $this->maxWidthThumb;
$maxHeight = $this->maxHeightThumb;
}
if ($maxWidth < $imgWidth || $maxHeight < $imgHeight) {
if ($maxWidth >= $maxHeight) {
$this->newWidth = round($maxHeight*($imgWidth/$imgHeight), 0);
$this->newHeight = round($maxHeight, 0);
} else {
$this->newWidth = round($maxWidth, 0);
$this->newHeight = round($maxWidth*($imgHeight/$imgWidth), 0);
}
if ($this->newWidth > $maxWidth) {
$this->newWidth = round($maxWidth, 0);
$this->newHeight = round($maxWidth*($imgHeight/$imgWidth), 0);
}
if ($this->newHeight > $maxHeight) {
$this->newWidth = round($maxHeight*($imgWidth/$imgHeight), 0);
$this->newHeight = round($maxHeight, 0);
}
} else {
$this->newWidth = round($imgWidth, 0);
$this->newHeight = round($imgHeight, 0);
}
}
function resize_GD() {
global $HTTP_POST_VARS;
if (!extension_loaded('gd')) {
if (!dl('gd.so')) {
$this->error('gdinstall');
}
}
foreach ($this->upload->uploadedFiles as $file) {
if ($this->makeThumb == "true"
$this->calculateSize($file->imageWidth, $file->imageHeight, "thumb"
$this->resize_file_GD($file, "thumb"
}
if ($this->resizeImages == "true"
$this->calculateSize($file->imageWidth, $file->imageHeight, "image"
$this->resize_file_GD($file, "image"
$file->setImageSize($this->newWidth, $this->newHeight);
$HTTP_POST_VARS[$this->upload->saveWidth] = $this->newWidth;
$HTTP_POST_VARS[$this->upload->saveHeight] = $this->newHeight;
}
}
}
function resize_file_GD($file, $create) {
$gdfuncs = get_extension_funcs("gd"
$imageInfo = @getimagesize($this->upload->path.'/'.$file->fileName);
switch ($imageInfo[2]) {
case 1:
// tot GD Library 1.6
if (!array_search("imagecreatefromgif", $gdfuncs)) {
$this->error('gdinvalid', 'gif');
}
$src_img = @imagecreatefromgif($this->upload->path.'/'.$file->fileName);
break;
case 2:
// vanaf PHP 3.0.16
if (!array_search("imagecreatefromjpeg", $gdfuncs)) {
$this->error('gdinvalid', 'jpeg');
}
$src_img = @imagecreatefromjpeg($this->upload->path.'/'.$file->fileName);
break;
case 3:
// vanaf PHP 3.0.13
if (!array_search("imagecreatefrompng", $gdfuncs)) {
$this->error('gdinvalid', 'png');
}
$src_img = @imagecreatefrompng($this->upload->path.'/'.$file->fileName);
break;
default:
$this->error('invalid');
break;
}
if ($this->check_php_version('4.0.6')) {
// vanaf PHP 4.0.6 en GD Library 2.0.l (anders imagecopyresized en imagecreate)
$dst_img = @imagecreatetruecolor($this->newWidth,$this->newHeight);
@imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $file->imageWidth, $file->imageHeight);
}
if (!$dst_img) {
$dst_img = imagecreate($this->newWidth,$this->newHeight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $file->imageWidth, $file->imageHeight);
}
if ($create == "image"
$fileName = $file->name.".jpg";
unlink($this->upload->path.'/'.$file->fileName);
imagejpeg($dst_img, $this->upload->path.'/'.$fileName, $this->quality); // vanaf PHP 3.0.16
$file->setFileName($fileName);
} else {
$fileName = $this->prefix.$file->name.".jpg";
imagejpeg($dst_img, $this->upload->path.'/'.$fileName, $this->quality); // vanaf PHP 3.0.16
}
imagedestroy($src_img);
imagedestroy($dst_img);
}
function resize_NetPBM() {
global $HTTP_POST_VARS;
foreach ($this->upload->uploadedFiles as $file) {
if ($this->makeThumb == "true"
$this->calculateSize($file->imageWidth, $file->imageHeight, "thumb"
$this->resize_file_NetPBM($file, "thumb"
}
if ($this->resizeImages == "true"
$this->calculateSize($file->imageWidth, $file->imageHeight, "image"
$this->resize_file_NetPBM($file, "image"
$file->setImageSize($this->newWidth, $this->newHeight);
$HTTP_POST_VARS[$this->upload->saveWidth] = $this->newWidth;
$HTTP_POST_VARS[$this->upload->saveHeight] = $this->newHeight;
}
}
}
function resize_file_NetPBM($file, $create) {
$imageInfo = @getimagesize($this->upload->path.'/'.$file->fileName);
switch ($imageInfo[2]) {
case 1:
// GIF
if (!exec(dirname(__FILE__)."/giftopnm ".$this->upload->path.'/'.$file->fileName)) {
$this->error('netpbm');
}
break;
case 2:
// JPEG
if (!exec(dirname(__FILE__)."/jpegtopnm ".$this->upload->path.'/'.$file->fileName)) {
$this->error('netpbm');
}
break;
case 3:
// PNG
if (!exec(dirname(__FILE__)."/pngtopnm ".$this->upload->path.'/'.$file->fileName)) {
$this->error('netpbm');
}
break;
default:
$this->error('invalid');
break;
}
exec(dirname(__FILE__)."/pnmscale -xysize ".$this->newWidth." ".$this->newHeight);
if ($create == "image"
$fileName = $file->name.".jpg";
unlink($this->upload->path.'/'.$file->fileName);
exec(dirname(__FILE__)."/ppmtojpeg -quality=".$this->quality." ".$this->upload->path.'/'.$fileName);
$file->setFileName($fileName);
} else {
$fileName = $this->prefix.$file->name.".jpg";
exec(dirname(__FILE__)."/ppmtojpeg -quality=".$this->quality." ".$this->upload->path.'/'.$fileName);
}
}
function check_php_version($version)
{
$testVer=intval(str_replace(".", "",$version));
$curVer=intval(str_replace(".", "",phpversion()));
if( $curVer < $testVer )
return false;
return true;
}
function error($error, $extra) {
// Display error
echo "<b>Error accured in the Smart Image Processor</b><br/><br/>";
switch ($error) {
// Error renaming the file
case 'invalid':
echo "The uploaded file is not an valid image format that is supported<br/>";
break;
// Error with netpbm
case 'netpbm':
echo "There is an error accured with the NetPBM Component<br/>";
break;
// Error renaming the file
case 'gdinvalid':
echo "The GD Library that is installed does not support ".$extra."<br/>";
break;
// GD Library is not found
case 'gdinstall':
echo "The GD Library is not installed correctly<br/>";
break;
}
// Allow to go back and stop the script
echo "Please correct and <a href=\"javascript:history.back(1)\">try again</a>";
$this->upload->failUpload();
exit;
}
function cleanUp() {
foreach ($this->upload->uploadedFiles as $file) {
$fileName = $this->prefix.$file->name.".jpg";
@unlink($filename);
}
}
}
?>
