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-&gt;upload-&gt;registerAddOn($this);
}

function doResize() {
switch ($this-&gt;component) {
case 'GD':
$this-&gt;resize_GD();
break;
case 'NetPBM':
$this-&gt;resize_NetPBM();
break;
}
}

function calculateSize($imgWidth, $imgHeight, $create) {
if ($create == "image" {
$maxWidth = $this-&gt;maxWidth;
$maxHeight = $this-&gt;maxHeight;
} else {
$maxWidth = $this-&gt;maxWidthThumb;
$maxHeight = $this-&gt;maxHeightThumb;
}
if ($maxWidth &lt; $imgWidth || $maxHeight &lt; $imgHeight) {
if ($maxWidth &gt;= $maxHeight) {
$this-&gt;newWidth = round($maxHeight*($imgWidth/$imgHeight), 0);
$this-&gt;newHeight = round($maxHeight, 0);
} else {
$this-&gt;newWidth = round($maxWidth, 0);
$this-&gt;newHeight = round($maxWidth*($imgHeight/$imgWidth), 0);
}
if ($this-&gt;newWidth &gt; $maxWidth) {
$this-&gt;newWidth = round($maxWidth, 0);
$this-&gt;newHeight = round($maxWidth*($imgHeight/$imgWidth), 0);
}
if ($this-&gt;newHeight &gt; $maxHeight) {
$this-&gt;newWidth = round($maxHeight*($imgWidth/$imgHeight), 0);
$this-&gt;newHeight = round($maxHeight, 0);
}
} else {
$this-&gt;newWidth = round($imgWidth, 0);
$this-&gt;newHeight = round($imgHeight, 0);
}
}

function resize_GD() {
global $HTTP_POST_VARS;
if (!extension_loaded('gd')) {
if (!dl('gd.so')) {
$this-&gt;error('gdinstall');
}
}
foreach ($this-&gt;upload-&gt;uploadedFiles as $file) {
if ($this-&gt;makeThumb == "true" {
$this-&gt;calculateSize($file-&gt;imageWidth, $file-&gt;imageHeight, "thumb";
$this-&gt;resize_file_GD($file, "thumb";
}
if ($this-&gt;resizeImages == "true" {
$this-&gt;calculateSize($file-&gt;imageWidth, $file-&gt;imageHeight, "image";
$this-&gt;resize_file_GD($file, "image";
$file-&gt;setImageSize($this-&gt;newWidth, $this-&gt;newHeight);
$HTTP_POST_VARS[$this-&gt;upload-&gt;saveWidth] = $this-&gt;newWidth;
$HTTP_POST_VARS[$this-&gt;upload-&gt;saveHeight] = $this-&gt;newHeight;
}
}
}

function resize_file_GD($file, $create) {
$gdfuncs = get_extension_funcs("gd";
$imageInfo = @getimagesize($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
switch ($imageInfo[2]) {
case 1:
// tot GD Library 1.6
if (!array_search("imagecreatefromgif", $gdfuncs)) {
$this-&gt;error('gdinvalid', 'gif');
}
$src_img = @imagecreatefromgif($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
break;
case 2:
// vanaf PHP 3.0.16
if (!array_search("imagecreatefromjpeg", $gdfuncs)) {
$this-&gt;error('gdinvalid', 'jpeg');
}
$src_img = @imagecreatefromjpeg($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
break;
case 3:
// vanaf PHP 3.0.13
if (!array_search("imagecreatefrompng", $gdfuncs)) {
$this-&gt;error('gdinvalid', 'png');
}
$src_img = @imagecreatefrompng($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
break;
default:
$this-&gt;error('invalid');
break;
}
if ($this-&gt;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-&gt;newWidth,$this-&gt;newHeight);
@imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $this-&gt;newWidth, $this-&gt;newHeight, $file-&gt;imageWidth, $file-&gt;imageHeight);
}
if (!$dst_img) {
$dst_img = imagecreate($this-&gt;newWidth,$this-&gt;newHeight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $this-&gt;newWidth, $this-&gt;newHeight, $file-&gt;imageWidth, $file-&gt;imageHeight);
}
if ($create == "image" {
$fileName = $file-&gt;name.".jpg";
unlink($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
imagejpeg($dst_img, $this-&gt;upload-&gt;path.'/'.$fileName, $this-&gt;quality); // vanaf PHP 3.0.16
$file-&gt;setFileName($fileName);
} else {
$fileName = $this-&gt;prefix.$file-&gt;name.".jpg";
imagejpeg($dst_img, $this-&gt;upload-&gt;path.'/'.$fileName, $this-&gt;quality); // vanaf PHP 3.0.16
}
imagedestroy($src_img);
imagedestroy($dst_img);
}

function resize_NetPBM() {
global $HTTP_POST_VARS;
foreach ($this-&gt;upload-&gt;uploadedFiles as $file) {
if ($this-&gt;makeThumb == "true" {
$this-&gt;calculateSize($file-&gt;imageWidth, $file-&gt;imageHeight, "thumb";
$this-&gt;resize_file_NetPBM($file, "thumb";
}
if ($this-&gt;resizeImages == "true" {
$this-&gt;calculateSize($file-&gt;imageWidth, $file-&gt;imageHeight, "image";
$this-&gt;resize_file_NetPBM($file, "image";
$file-&gt;setImageSize($this-&gt;newWidth, $this-&gt;newHeight);
$HTTP_POST_VARS[$this-&gt;upload-&gt;saveWidth] = $this-&gt;newWidth;
$HTTP_POST_VARS[$this-&gt;upload-&gt;saveHeight] = $this-&gt;newHeight;

}
}
}

function resize_file_NetPBM($file, $create) {
$imageInfo = @getimagesize($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
switch ($imageInfo[2]) {
case 1:
// GIF
if (!exec(dirname(__FILE__)."/giftopnm ".$this-&gt;upload-&gt;path.'/'.$file-&gt;fileName)) {
$this-&gt;error('netpbm');
}
break;
case 2:
// JPEG
if (!exec(dirname(__FILE__)."/jpegtopnm ".$this-&gt;upload-&gt;path.'/'.$file-&gt;fileName)) {
$this-&gt;error('netpbm');
}
break;
case 3:
// PNG
if (!exec(dirname(__FILE__)."/pngtopnm ".$this-&gt;upload-&gt;path.'/'.$file-&gt;fileName)) {
$this-&gt;error('netpbm');
}
break;
default:
$this-&gt;error('invalid');
break;
}
exec(dirname(__FILE__)."/pnmscale -xysize ".$this-&gt;newWidth." ".$this-&gt;newHeight);
if ($create == "image" {
$fileName = $file-&gt;name.".jpg";
unlink($this-&gt;upload-&gt;path.'/'.$file-&gt;fileName);
exec(dirname(__FILE__)."/ppmtojpeg -quality=".$this-&gt;quality." ".$this-&gt;upload-&gt;path.'/'.$fileName);
$file-&gt;setFileName($fileName);
} else {
$fileName = $this-&gt;prefix.$file-&gt;name.".jpg";
exec(dirname(__FILE__)."/ppmtojpeg -quality=".$this-&gt;quality." ".$this-&gt;upload-&gt;path.'/'.$fileName);
}
}

function check_php_version($version)
{
$testVer=intval(str_replace(".", "",$version));
$curVer=intval(str_replace(".", "",phpversion()));
if( $curVer &lt; $testVer )
return false;
return true;
}

function error($error, $extra) {
// Display error
echo "&lt;b&gt;Error accured in the Smart Image Processor&lt;/b&gt;&lt;br/&gt;&lt;br/&gt;";

switch ($error) {
// Error renaming the file
case 'invalid':
echo "The uploaded file is not an valid image format that is supported&lt;br/&gt;";
break;

// Error with netpbm
case 'netpbm':
echo "There is an error accured with the NetPBM Component&lt;br/&gt;";
break;

// Error renaming the file
case 'gdinvalid':
echo "The GD Library that is installed does not support ".$extra."&lt;br/&gt;";
break;

// GD Library is not found
case 'gdinstall':
echo "The GD Library is not installed correctly&lt;br/&gt;";
break;
}

// Allow to go back and stop the script
echo "Please correct and &lt;a href=\"javascript:history.back(1)\"&gt;try again&lt;/a&gt;";
$this-&gt;upload-&gt;failUpload();
exit;
}

function cleanUp() {
foreach ($this-&gt;upload-&gt;uploadedFiles as $file) {
$fileName = $this-&gt;prefix.$file-&gt;name.".jpg";
@unlink($filename);
}
}
}

?&gt;

Reply to this topic