phpnewbie
04-03-2004, 09:53 AM
Hi,
I came across the resize_image() script from Coppermine (below)and tried it using GD2, it works great!
Only thing is I can only get it to resize a maximum of 2 images at a time, I've tried changing the some variables but still no joy.
I was hoping to use it to resize a list of image stored in a MySQL DB and write them to an images directory, any suggestions??:rolleyes:
<?php
/**
* resize_image()
*
* Create a file containing a resized image
*
* @param $src_file the source file
* @param $dest_file the destination file
* @param $new_size the size of the square within which the new image must fit
* @param $method the method used for image resizing
* @return 'true' in case of success
**/
/**
Variables
**/
include("mainfile.php");
$src_file = "testfile.jpg";
$method = "gd2";
$dest_file = "T_".$src_file."";
$new_size = "150";
$jpeg_qual = "80";
$file_mode = "0644";
resize_image($src_file, $dest_file, $new_size, $method);
// The actual Function to resize images using GD1, & GD2.......
function resize_image($src_file, $dest_file, $new_size, $method) {
global $CONFIG, $method, $file_mode, $jpeg_qual;
$imginfo = getimagesize($src_file);
if ($imginfo == null)
return false;
// GD can only handle JPG & PNG images
if ($imginfo[2] != 2 && $imginfo[2] != 3 && ($method == 'gd1' || $method == 'gd2')){
echo "Invalid filetype";
return false;
}
// height/width
$srcWidth = $imginfo[0];
$srcHeight = $imginfo[1];
$ratio = max($srcWidth, $srcHeight) / $new_size;
$ratio = max($ratio, 1.0);
$destWidth = (int)($srcWidth / $ratio);
$destHeight = (int)($srcHeight / $ratio);
// Method for thumbnails creation
switch ($method) {
case "gd1" :
echo "Converted using GD 1";
if (!function_exists('imagecreatefromjpeg')) {
die("PHP running on your server does not support the GD image library, check with your webhost if they can setup PHP with GD Library.");
}
if ($imginfo[2] == GIS_JPG)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
echo "invalid_image";
return false;
}
$dst_img = imagecreate($destWidth, $destHeight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
case "gd2" :
echo "Converted using GD 2";
if (!function_exists('imagecreatefromjpeg')) {
die("PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed");
}
if (!function_exists('imagecreatetruecolor')) {
die("PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page");
}
if ($imginfo[2] == 2)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
echo "invalid_image";
return false;
}
$dst_img = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
}
// Set mode of uploaded picture
chmod($dest_file, octdec($file_mode));
// We check that the image is valid
$imginfo = getimagesize($dest_file);
if ($imginfo == null){
echo "resize_failed";
@unlink($dest_file);
return false;
} else {
return true;
}
}
?>
I came across the resize_image() script from Coppermine (below)and tried it using GD2, it works great!
Only thing is I can only get it to resize a maximum of 2 images at a time, I've tried changing the some variables but still no joy.
I was hoping to use it to resize a list of image stored in a MySQL DB and write them to an images directory, any suggestions??:rolleyes:
<?php
/**
* resize_image()
*
* Create a file containing a resized image
*
* @param $src_file the source file
* @param $dest_file the destination file
* @param $new_size the size of the square within which the new image must fit
* @param $method the method used for image resizing
* @return 'true' in case of success
**/
/**
Variables
**/
include("mainfile.php");
$src_file = "testfile.jpg";
$method = "gd2";
$dest_file = "T_".$src_file."";
$new_size = "150";
$jpeg_qual = "80";
$file_mode = "0644";
resize_image($src_file, $dest_file, $new_size, $method);
// The actual Function to resize images using GD1, & GD2.......
function resize_image($src_file, $dest_file, $new_size, $method) {
global $CONFIG, $method, $file_mode, $jpeg_qual;
$imginfo = getimagesize($src_file);
if ($imginfo == null)
return false;
// GD can only handle JPG & PNG images
if ($imginfo[2] != 2 && $imginfo[2] != 3 && ($method == 'gd1' || $method == 'gd2')){
echo "Invalid filetype";
return false;
}
// height/width
$srcWidth = $imginfo[0];
$srcHeight = $imginfo[1];
$ratio = max($srcWidth, $srcHeight) / $new_size;
$ratio = max($ratio, 1.0);
$destWidth = (int)($srcWidth / $ratio);
$destHeight = (int)($srcHeight / $ratio);
// Method for thumbnails creation
switch ($method) {
case "gd1" :
echo "Converted using GD 1";
if (!function_exists('imagecreatefromjpeg')) {
die("PHP running on your server does not support the GD image library, check with your webhost if they can setup PHP with GD Library.");
}
if ($imginfo[2] == GIS_JPG)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
echo "invalid_image";
return false;
}
$dst_img = imagecreate($destWidth, $destHeight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
case "gd2" :
echo "Converted using GD 2";
if (!function_exists('imagecreatefromjpeg')) {
die("PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed");
}
if (!function_exists('imagecreatetruecolor')) {
die("PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page");
}
if ($imginfo[2] == 2)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
echo "invalid_image";
return false;
}
$dst_img = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
}
// Set mode of uploaded picture
chmod($dest_file, octdec($file_mode));
// We check that the image is valid
$imginfo = getimagesize($dest_file);
if ($imginfo == null){
echo "resize_failed";
@unlink($dest_file);
return false;
} else {
return true;
}
}
?>
