2006-12-02 04:36:16 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2009-11-12 15:11:17 +00:00
|
|
|
* e107 website system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2009 e107 Inc (e107.org)
|
|
|
|
* Released under the terms and conditions of the
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* $Source: /cvs_backup/e107_0.8/e107_handlers/resize_handler.php,v $
|
|
|
|
* $Revision: 1.12 $
|
|
|
|
* $Date: 2009-11-12 15:11:16 $
|
|
|
|
* $Author: marj_nl_fr $
|
|
|
|
*/
|
|
|
|
|
2006-12-02 04:36:16 +00:00
|
|
|
if (!defined('e107_INIT')) { exit; }
|
|
|
|
|
2009-04-23 20:23:49 +00:00
|
|
|
// Given an image file name, return the mime type string. Returns FALSE if invalid
|
|
|
|
function mimeFromFilename($fileName)
|
|
|
|
{
|
|
|
|
$fileExt = strtolower(substr(strrchr($fileName, "."), 1));
|
|
|
|
$mimeTypes = array(
|
|
|
|
'jpg' => 'jpeg',
|
|
|
|
'gif' => 'gif',
|
|
|
|
'png' => 'png',
|
|
|
|
'jpeg' => 'jpeg',
|
|
|
|
'pjpeg' => 'jpeg',
|
|
|
|
'bmp' => 'bmp'
|
|
|
|
);
|
|
|
|
if (!isset($mimeTypes[$fileExt])) { return FALSE; } // only allow image files }
|
|
|
|
return "Content-type: image/".$mimeTypes[$fileExt];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-13 21:45:46 +00:00
|
|
|
function resize_image($source_file, $destination_file, $type = "upload", $model = "")
|
|
|
|
{
|
|
|
|
// $destination_file - 'stdout' sends direct to browser. Otherwise treated as file name
|
2007-07-08 21:01:00 +00:00
|
|
|
// - if its a file, given '644' permissions
|
2008-10-03 20:28:54 +00:00
|
|
|
// $type - numeric - sets new width of image
|
|
|
|
// - "upload" - uses preference 'im_width', or 400px if not defined
|
|
|
|
// - anything else - default preference for image width & heightused, or failing that, 120 px x 100 px
|
|
|
|
// 'avatar' may be used to invoke the default (see usersettings.php)
|
|
|
|
// $model - "copy" - creates a new file for the destination, by prefixing $destination_file with 'thumb_'. Return error for small images.
|
|
|
|
// - 'upsize' - small images are enlarged
|
|
|
|
// - 'noscale' - small images are transferred at their original size
|
|
|
|
// - 'nocopy' - used in content manager plugin
|
2007-07-08 21:01:00 +00:00
|
|
|
// Otherwise overwrites any existing $destination_file
|
|
|
|
|
|
|
|
// Returns: TRUE - essentially, if $destination_file (or a file with a modified name) is valid:
|
|
|
|
// - if resizing done
|
|
|
|
// - source and (ultimate) destination files are the same, and the image was smaller than the limits
|
|
|
|
// - destination was 'stdout', and file output successfully
|
|
|
|
// FALSE - essentially, if there is not a valid output file available - usually, that resizing failed, or some other error.
|
|
|
|
|
2006-12-02 04:36:16 +00:00
|
|
|
global $pref;
|
2009-04-23 20:23:49 +00:00
|
|
|
|
2006-12-02 04:36:16 +00:00
|
|
|
$new_height = 0;
|
|
|
|
$mode = ($pref['resize_method'] ? $pref['resize_method'] : "gd2");
|
2007-04-13 21:45:46 +00:00
|
|
|
if ($type == "upload")
|
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
$new_size = varset($pref['im_width'],400);
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2007-04-13 21:45:46 +00:00
|
|
|
elseif(is_numeric($type))
|
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
$new_size = $type;
|
2007-04-13 21:45:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Use preferences or failing that hard-coded defaults for new size
|
2009-04-23 20:23:49 +00:00
|
|
|
$new_size = varset($pref['im_width'], 120);
|
|
|
|
$new_height = varset($pref['im_height'], 100);
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-04-23 20:23:49 +00:00
|
|
|
|
2008-10-03 20:28:54 +00:00
|
|
|
|
|
|
|
$im_quality = varset($pref['im_quality'], 99);
|
|
|
|
|
2009-04-23 20:23:49 +00:00
|
|
|
|
2006-12-02 04:36:16 +00:00
|
|
|
$image_stats = getimagesize($source_file);
|
2007-04-13 21:45:46 +00:00
|
|
|
if ($image_stats == null)
|
|
|
|
{
|
2007-06-25 20:09:30 +00:00
|
|
|
// echo "<b>DEBUG</b> image_stats are null<br />";
|
2009-04-23 20:23:49 +00:00
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2007-07-08 21:01:00 +00:00
|
|
|
if (($image_stats[0] == 0) || ($image_stats[1] == 0))
|
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
return FALSE; // Zero sized image - shouldn't happen
|
2007-07-08 21:01:00 +00:00
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
2007-07-08 21:01:00 +00:00
|
|
|
// Check the image type. '1'=GIF, '2'=jpeg, '3' = PNG
|
2007-04-13 21:45:46 +00:00
|
|
|
if ($image_stats[2] != 1 && $image_stats[2] != 2 && $image_stats[2] != 3 && ($mode == 'gd1' || $mode == 'gd2'))
|
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
echo "<b>DEBUG</b> Wrong image type<br />";
|
|
|
|
return FALSE;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2007-07-08 21:01:00 +00:00
|
|
|
|
2007-04-13 21:45:46 +00:00
|
|
|
$imagewidth = $image_stats[0]; // Width of existing image
|
|
|
|
$imageheight = $image_stats[1]; // Height of existing image
|
|
|
|
if ($imagewidth <= $new_size && ($imageheight <= $new_height || $new_height == 0))
|
|
|
|
{ // Nothing to do if image width already smaller than the maximum
|
2007-07-08 21:01:00 +00:00
|
|
|
// If we were basically ensuring an existing file was within limits, return TRUE
|
|
|
|
// If we had to create a new file, return FALSE since it wasn't done.
|
2008-10-03 20:28:54 +00:00
|
|
|
switch ($model)
|
|
|
|
{
|
|
|
|
case 'copy' : // Not sure what to do here!
|
|
|
|
return FALSE; // This is what it used to do
|
|
|
|
break;
|
|
|
|
case 'upsize' : // Scale source up to required size
|
|
|
|
break; // Just fall through to do that.
|
|
|
|
case 'noscale' : // No scaling of small images- just want destination to be the same as source
|
|
|
|
if ($destination_file == 'stdout')
|
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
if (($result = mimeFromFilename($source_file)) === FALSE) { return FALSE; }
|
|
|
|
header($result);
|
2008-10-03 20:28:54 +00:00
|
|
|
if (@readfile($source_file) === FALSE) { return FALSE; }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return copy($source_file,$destination_file);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
return ($source_file == $destination_file);
|
|
|
|
}
|
|
|
|
// return (($source_file == $destination_file) && ($model != 'copy'));
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2007-04-13 21:45:46 +00:00
|
|
|
|
2006-12-02 04:36:16 +00:00
|
|
|
$ratio = ($imagewidth / $new_size);
|
|
|
|
$new_imageheight = round($imageheight / $ratio);
|
2007-04-13 21:45:46 +00:00
|
|
|
if (($new_height <= $new_imageheight) && $new_height > 0)
|
|
|
|
{
|
2006-12-02 04:36:16 +00:00
|
|
|
$ratio = $new_imageheight / $new_height;
|
|
|
|
$new_imageheight = $new_height;
|
|
|
|
$new_size = round($new_size / $ratio);
|
|
|
|
|
|
|
|
}
|
2007-04-13 21:45:46 +00:00
|
|
|
|
2008-10-03 20:28:54 +00:00
|
|
|
if (($destination_file != 'stdout') && ($model == 'copy'))
|
|
|
|
{
|
|
|
|
$destination_file = dirname($destination_file).'/thumb_'.basename($destination_file);
|
|
|
|
}
|
|
|
|
$returnError = 0; // Return value from some of the commands
|
2007-04-13 21:45:46 +00:00
|
|
|
switch ($mode)
|
|
|
|
{
|
|
|
|
case "ImageMagick" :
|
|
|
|
if ($destination_file == "stdout")
|
2008-10-03 20:28:54 +00:00
|
|
|
{ // if destination is stdout, output directly to the browser
|
2007-04-13 21:45:46 +00:00
|
|
|
// $destination_file = "jpg:-";
|
|
|
|
header("Content-type: image/jpeg");
|
|
|
|
// Use double quotes instead of single to keep Bill happy
|
2008-10-03 20:28:54 +00:00
|
|
|
passthru ($pref['im_path']."convert -quality ".$im_quality." -antialias -geometry ".$new_size."x".$new_imageheight." ".escapeshellarg($source_file)." \"jpg:-\"", $returnError);
|
2007-04-13 21:45:46 +00:00
|
|
|
}
|
|
|
|
else
|
2008-10-03 20:28:54 +00:00
|
|
|
{ // otherwise output to file
|
2007-04-13 21:45:46 +00:00
|
|
|
// Use double quotes instead of single to keep Bill happy
|
2008-10-03 20:28:54 +00:00
|
|
|
exec ($pref['im_path']."convert -quality ".$im_quality." -antialias -geometry ".$new_size."x".$new_imageheight." ".escapeshellarg($source_file)." \"".$destination_file."\"", $dummy, $returnError);
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-10-03 20:28:54 +00:00
|
|
|
if ($returnError) echo "ImageMagick resize/output error: {$returnError}<br />";
|
2007-12-20 20:51:17 +00:00
|
|
|
break;
|
2008-10-03 20:28:54 +00:00
|
|
|
case 'gd1' :
|
|
|
|
case 'gd2' :
|
2007-05-29 18:56:53 +00:00
|
|
|
switch ($image_stats[2])
|
|
|
|
{
|
|
|
|
case IMAGETYPE_PNG : // 3 - PNG
|
2007-12-20 20:51:17 +00:00
|
|
|
$src_img = @imagecreatefrompng($source_file);
|
2009-04-23 20:23:49 +00:00
|
|
|
$fileExt = 'png';
|
2007-05-29 18:56:53 +00:00
|
|
|
break;
|
|
|
|
case IMAGETYPE_GIF : // 1 - GIF
|
|
|
|
if (!function_exists('imagecreatefromgif')) return FALSE; // Some versions of GD library don't support GIF
|
2007-12-20 20:51:17 +00:00
|
|
|
$src_img = @imagecreatefromgif($source_file);
|
2009-04-23 20:23:49 +00:00
|
|
|
$fileExt = 'gif';
|
2007-05-29 18:56:53 +00:00
|
|
|
break;
|
|
|
|
case IMAGETYPE_JPEG : // 2 - Jpeg
|
2007-12-20 20:51:17 +00:00
|
|
|
$src_img = @imagecreatefromjpeg($source_file);
|
2009-04-23 20:23:49 +00:00
|
|
|
$fileExt = 'jpg';
|
2007-05-29 18:56:53 +00:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
return FALSE; // Unsupported image type
|
|
|
|
}
|
2007-04-13 21:45:46 +00:00
|
|
|
if (!$src_img)
|
|
|
|
{
|
|
|
|
return FALSE;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2007-04-13 21:45:46 +00:00
|
|
|
// Only next line is different between gd1 and gd2
|
2008-10-03 20:28:54 +00:00
|
|
|
if ($mode == 'gd1')
|
2007-04-13 21:45:46 +00:00
|
|
|
{
|
2008-10-03 20:28:54 +00:00
|
|
|
$dst_img = imagecreate($new_size, $new_imageheight); // Create blank image of correct size as target
|
|
|
|
if (!imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_size, $new_imageheight, $imagewidth, $imageheight)) { $returnError = -4; }
|
2009-04-23 20:23:49 +00:00
|
|
|
}
|
|
|
|
else
|
2007-04-13 21:45:46 +00:00
|
|
|
{
|
2008-10-03 20:28:54 +00:00
|
|
|
$dst_img = imagecreatetruecolor($new_size, $new_imageheight);
|
|
|
|
if (!imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_size, $new_imageheight, $imagewidth, $imageheight)) { $returnError = -5; }
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-10-03 20:28:54 +00:00
|
|
|
if ($returnError)
|
2007-04-13 21:45:46 +00:00
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
echo "Resizing error (1): {$returnError}<br />";
|
|
|
|
return FALSE;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-04-23 20:23:49 +00:00
|
|
|
|
|
|
|
// Now output or save the resized file
|
|
|
|
|
|
|
|
$destName = $destination_file;
|
2007-04-13 21:45:46 +00:00
|
|
|
if ($destination_file == "stdout")
|
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
$destName = '';
|
|
|
|
if (($result = mimeFromFilename($source_file)) === FALSE)
|
|
|
|
{
|
|
|
|
$returnError = -6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
header($result);
|
|
|
|
}
|
2007-04-13 21:45:46 +00:00
|
|
|
}
|
2009-04-23 20:23:49 +00:00
|
|
|
else
|
2007-04-13 21:45:46 +00:00
|
|
|
{
|
2009-04-23 20:23:49 +00:00
|
|
|
$fileExt = strtolower(substr(strrchr($destination_file, "."), 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($returnError == 0)
|
|
|
|
{ // We can output the image, or save it to a file
|
|
|
|
switch ($fileExt)
|
|
|
|
{
|
|
|
|
case 'png' :
|
|
|
|
if (!imagepng($dst_img, $destName, 6)) { $returnError = -1; } // Fix the quality for now
|
2009-09-19 20:46:19 +00:00
|
|
|
$outputFunc = 'imagepng';
|
2009-04-23 20:23:49 +00:00
|
|
|
break;
|
|
|
|
case 'gif' :
|
|
|
|
if (!imagegif($dst_img, $destName)) { $returnError = -1; }
|
|
|
|
$outputFunc = 'imagegif';
|
|
|
|
break;
|
|
|
|
case 'jpg' :
|
|
|
|
case 'jpeg' :
|
|
|
|
if (!imagejpeg($dst_img, $destName, $im_quality)) { $returnError = -1; }
|
|
|
|
$outputFunc = 'imagejpeg';
|
|
|
|
break;
|
|
|
|
default :
|
2009-09-19 20:46:19 +00:00
|
|
|
$returnError = -7; // Invalid output extension
|
|
|
|
$outputFunc = 'none';
|
2009-04-23 20:23:49 +00:00
|
|
|
}
|
2008-10-03 20:28:54 +00:00
|
|
|
}
|
2009-04-23 20:23:49 +00:00
|
|
|
|
2008-10-03 20:28:54 +00:00
|
|
|
if (!imagedestroy($src_img)) { $returnError = -2; }
|
|
|
|
if (!imagedestroy($dst_img)) { $returnError = -3; }
|
|
|
|
if ($returnError)
|
|
|
|
{
|
2009-09-19 20:46:19 +00:00
|
|
|
echo "Resizing error (2): {$returnError} - {$outputFunc} -> {$destName}<br />";
|
2008-10-03 20:28:54 +00:00
|
|
|
return FALSE;
|
2009-04-23 20:23:49 +00:00
|
|
|
}
|
2007-10-10 21:34:44 +00:00
|
|
|
break;
|
2008-10-03 20:28:54 +00:00
|
|
|
default :
|
|
|
|
echo "Invalid resize function: {$mode}<br />";
|
|
|
|
return FALSE;
|
2007-04-13 21:45:46 +00:00
|
|
|
} // End switch($mode)
|
|
|
|
|
|
|
|
if ($destination_file == "stdout") return TRUE; // Can't do anything more if file sent to stdout - assume success
|
2006-12-02 04:36:16 +00:00
|
|
|
|
|
|
|
@chmod($destination_file, 0644);
|
2009-04-23 20:23:49 +00:00
|
|
|
if ($pref['image_owner'])
|
|
|
|
{
|
|
|
|
@chown($destination_file, $pref['image_owner']);
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
|
|
|
$image_stats = getimagesize($destination_file);
|
2007-04-13 21:45:46 +00:00
|
|
|
if ($image_stats == null)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return TRUE;
|
2009-04-23 20:23:49 +00:00
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
|
|
|
?>
|