Welcome to WebmasterWorld Guest from 50.16.84.67

Forum Moderators: coopster & jatar k

Resizing an image - question on if/then

   
6:54 am on Jun 4, 2008 (gmt 0)

5+ Year Member



Okay - so I have a simple image uploader - that makes a thumbnail - and resizes the original image if it is too large. My problem is that even though the program works - and resizes the original image - I don't want the image to be resized if it is SMALLER than the max width/height of 400 pixels. How would I write an if/then statement in there that would only resize the original if it was LARGER than 400/400? Because now - it resizes even tiny images to 400/400 pixels, and they come out really blurry when they are displayed. Any thoughts?


// upload the file if it exists
$file= uploadImage($_POST["uploadform"]);
// Create a Thumbnail if an image exists
if ($file != "no file")
{
$date = date("YmdHis");
$imgArr = split('[/]', $file);
$imgNameOnly = $imgArr[sizeof($imgArr)-1];
$folderPath = "";
for ($i=0; $i<sizeof($imgArr)-1; $i++)
$folderPath .= $imgArr[$i] . "/" ;

$photoPathName = $folderPath . $date . "_" . $imgNameOnly;
$thumbNailPathName = $folderPath . "thumb_" . $date . "_" . $imgNameOnly;
$imgType = getImgType($imgNameOnly);
// Create a resized image of the orig. a mx of 400 pixels
[b]$photo=new Thumbnail(400,400);[/b]
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$photo->loadData($image,$imgType);
// Build the thumbnail and store as a file
$photo->buildThumb($photoPathName);

chmod("$photoPathName", 0666);

// Instantiate the thumbnail
$tn=new Thumbnail(100,100);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$tn->loadData($image,$imgType);
// Build the thumbnail and store as a file
$tn->buildThumb($thumbNailPathName);
// delete the source file that is potentially large
unlink($file);

chmod("$thumbNailPathName", 0666);
}

7:03 am on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Could you use getimagesize [uk2.php.net] to give you the dimensions?

Then you could use if/else to decide if you want to resize.

7:29 am on Jun 4, 2008 (gmt 0)

5+ Year Member



Would the following work?


// upload the file if it exists
$file= uploadImage($_POST["uploadform"]);
// Create a Thumbnail if an image exists
if ($file != "no file")
{
$date = date("YmdHis");
$imgArr = split('[/]', $file);
$imgNameOnly = $imgArr[sizeof($imgArr)-1];
$folderPath = "";
for ($i=0; $i<sizeof($imgArr)-1; $i++)
$folderPath .= $imgArr[$i] . "/" ;

$photoPathName = $folderPath . $date . "_" . $imgNameOnly;
$thumbNailPathName = $folderPath . "thumb_" . $date . "_" . $imgNameOnly;
$imgType = getImgType($imgNameOnly);
// Create a resized image of the orig. a mx of 400 pixels

$foo = getimagesize("$photo");
$width=$foo[0];

if($width < '400'){
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$photo->loadData($image,$imgType);
// Build the thumbnail and store as a file
$photo->buildThumb($photoPathName);

chmod("$photoPathName", 0666);

// Instantiate the thumbnail
$tn=new Thumbnail(100,100);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$tn->loadData($image,$imgType);
// Build the thumbnail and store as a file
$tn->buildThumb($thumbNailPathName);
// delete the source file that is potentially large
unlink($file);

chmod("$thumbNailPathName", 0666);
} else {

$photo=new Thumbnail(400,400);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$photo->loadData($image,$imgType);
// Build the thumbnail and store as a file
$photo->buildThumb($photoPathName);

chmod("$photoPathName", 0666);

// Instantiate the thumbnail
$tn=new Thumbnail(100,100);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$tn->loadData($image,$imgType);
// Build the thumbnail and store as a file
$tn->buildThumb($thumbNailPathName);
// delete the source file that is potentially large
unlink($file);

chmod("$thumbNailPathName", 0666);
}
\\above is my edit
}

8:13 am on Jun 4, 2008 (gmt 0)

5+ Year Member



$foo = getimagesize("urlgoeshere");
$picwidth=$foo[0];
$picheight=$foo[1];

echo $picheight;

I'm trying to test out the getimagesize function - and learn how it works. Afterwords - I'll implement it into the code I have already posted in other posts. I've tried the above code - with the url of an image - and it seems to work with outputting the image's height. So how would I implement those ideals into the code that I've posted in the first post? Maybe an if/else statement comparing a smaller then satement of 400 pixels? Any thoughts on how I can put this together? Thanks!

10:46 am on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



$foo = getimagesize("urlgoeshere");
$picwidth=$foo[0];
$picheight=$foo[1];

Will give you the width and height. So you would need to do something like -

if (($picwidth > 400) && ($picheight > 400)) {
// resize
}
else {
// image is smaller than 400 so no point in resizing.
}

You seem to be getting along fine with the getimagesize :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month