Welcome to WebmasterWorld Guest from 54.166.5.230

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Image resizing with PHP

need some pointers

     
2:31 pm on Dec 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 28, 2003
posts:869
votes: 0


I have a form which uploads some info and an image into a database.

I would like to create a resized version of said image, and either save it somewhere or simply display it on the page.

I have been having a look at some code but have not been getting anywhere with trying to piece something together.

I have used getimagesize() successfully to get info about the image and have tried using imagecopyresized() to display a resized copy, but with no luck.

Here is some sample code I was trying to adapt


// File and new size
$filename = '../path/to/my/file.jpg;
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);

I think part of the problem is the

header('Content-type: image/jpeg');
bit, if I remove it I get a load of weird code on the page, and if I leave it in I just get a page which echoes its own url. Help!
7:47 pm on Dec 2, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


have you tried testing the return values of the various functions you are using to see what failed?
12:36 am on Dec 3, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Make sure you have no output before or after your imagejpeg(). Blank lines before opening/closing <?php tags, anything. Also try
exit;
immediately after your imagejpeg().
8:24 pm on Dec 3, 2008 (gmt 0)

New User

5+ Year Member

joined:Oct 25, 2008
posts:17
votes: 0


Do you need this as a seperate script altogether, or can you do this in a main script and resize with the HTML <img> tag? Something like:

<?
// Image file
$filename = "path/to/file.jpg";

// Set width and height you want
$desiredWidth = ###px;
$desiredHeight = ###px;

// Image details
list ($width, $height) - getimagesize($filename);

// Resizes height/width proportionally to desired width
if ($width > $desiredWidth){
$height = ($height/$width)*$desiredWidth;
$width = $desiredWidth;
}

// Resizes height/width proportionally to desired width
if ($height > $desiredHeight){
$width = ($width/$height)*$desiredHeight;
$height = $desiredHeight;
}

// Display image on HTML page
echo "<img height='$height' width='$width' src='$filename' />";
?>

11:22 pm on Dec 3, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


or can you do this in a main script and resize with the HTML <img> tag?

This is very different to the OPs original query of using the GD library to actually create a new resized image and send this to the client. To be honest, using PHP to create a new image of the correct size is by far the better solution (over letting HTML do the resizing), particularly if this is then saved/cached server side.

$percent = 0.5;

With a linear scale factor of 0.5 you are potentially looking at a resized image which is 1/4 of its original size. To send the original image and let HTML do the work is an unnecessary bandwidth hog and the quality will be poor (in most browsers).

Using PHP to do the work will be quicker, better quality and will use less bandwidth - everyone's a winner.

5:05 am on Dec 4, 2008 (gmt 0)

New User

5+ Year Member

joined:Oct 25, 2008
posts:17
votes: 0


To be honest, using PHP to create a new image of the correct size is by far the better solution (over letting HTML do the resizing), particularly if this is then saved/cached server side.

Interesting. How about when calling an image from a non-local site, would it still be better to resize/recreate with php?

7:33 am on Dec 4, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 15, 2005
posts:79
votes: 0


Check this line:

$filename = '../path/to/my/file.jpg;

you need a closing '

$filename = '../path/to/my/file.jpg';

William.

3:34 pm on Dec 8, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 28, 2003
posts:869
votes: 0


Thanks for your replies guys. Getting rid of all the output before the header and using exit; did the trick :)

As I delve further into this project however, I think I really need to save a copy of the resized image rather than linking to it - how can I go about this?

12:02 pm on Jan 21, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 28, 2003
posts:869
votes: 0


For anyone who was interested in this topic, here is my end solution.

Thanks again for your help, it was nearly there, just needed to save the file rather than output it, and I swapped imagecopyresized for imagecopyresampled, which produces a much better looking image:

<?php

// File and new size
$filename = 'pathtofile/testpic.jpg';

//echo($filename);

// Get new sizes
list($width, $height) = getimagesize($filename);

//figure out height ratios

$newwidth = 200;
$newheight = ($newwidth/$width) * $height;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Save the image as 'mynewpic.jpg'
imagejpeg($thumb, 'pathtofile/mynewpic.jpg',85);

// Free up memory
imagedestroy($thumb);
?>
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members