
|
View Full Version : Resizing images using GD...
MGCJerry 08-23-2003, 08:27 PM I'm having difficulty with creating thumbnails in a script I'm working on.
I'm wanting to see if a specific image exists, and if it doesnt I want the script to automatically create a proportional thumbnail to a specific width, and saves it in a directory on the server.
Does anyone have any snippets similar to this that I could look at. I have really no clue how to go about doing this or know of any apps I could look at for ideas.
Thanks in advance.
Mesum 08-24-2003, 01:11 AM I use coppermine gallery and it uses GD really well, you might wanna look into that.
MGCJerry 08-24-2003, 11:03 AM I'm looking at the coppermine code, but I cant seem to get it to work. :bawling:
It doesnt create the file. :confused:
I'm a complete idiot to GD, so could anyone point out what I'm missing? I'm thinking it isnt paying attention to the switch.
Any ideas? Here's the code.
<?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;
}
}
?>
MGCJerry 08-24-2003, 01:14 PM Ok... I figured it out. I got it working just fine. :)
I have updated the above code.
eScaPedd 08-24-2003, 09:24 PM Maybe these codes can help you ^^
<?
$FILENAME="image_name";
$RESIZEWIDTH=400;
$RESIZEHEIGHT=400;
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
?>
<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br/><br/>
<form enctype="multipart/form-data" method="post">
<br/>
<input type="file" name="image" size="50" value="Browse"><p>
<input type="submit" value="upload">
</form>
</body>
</html>
phpnewbie 04-03-2004, 09:45 AM Hi,
I came across the resize_image() script from Coppermine 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??:confused:
orbitz 04-03-2004, 11:05 AM I've just played around with this resizing class, and it is really great. You can view the demo or download from the link below:
http://horobey.com/demos/index.html
This class has few nice options for you to create thumbnails. See the demo for yourself :)
With this script, you can probably add few lines to have it create many images stored in a directory at once.
phpnewbie 04-03-2004, 11:59 AM Hi,
wasn't looking to create thumbnails, I had been using a Class.thumbnail file to resize larger images and the resulting image quality was bad so looking for something better. The coppermine script does the job, as I've said it'll only process 2 files at a time and I can't seem to modify it, any suggestions?
orbitz 04-03-2004, 01:26 PM creating thumbnails and resizing are the same concept of changing the dimesions of an image :) YEs, i am using it to resize my images or to create new images based on the orginal one.
use the class above without modifying it... you just have to write few lines how to read files from a directory, and then change their sizes
phpnewbie 04-03-2004, 01:44 PM I hear ya!
The function above that was taken form the coppermine image gallery
**
* resize_image()
*
* Create a file containing a resized image.....
is the only one that I've seen can create quality resized images using GD. I was using a Class.Thumbnail to resized images but it was designed for thumbnails so for larger images it didn't work well. I tried inserting...
ImageJpeg($new, $old, 100)
...into Class.Thumbnail to increase the JPEG file quality but to no avail. I was hoping to be able to use the coppermine script to do the job for me but it only does 2 files at a time and how to modify it to increase the amount of files it'll create I haven't a clue, am new to programming and PHP!!:eek:
|