sam928
asked on
PHP -- HOw to Resize a JPG Image proportionately on upload function..
Hello,
I have PHP script that resizes a JPG image (from an upload function)
However, the script that I have DOES NOT resize the image PROPORTIONANTLEY, (sorry for spelling ;) )
The script i have attached resizes the image to a height of 100 pixels. and it changes the width to 500.. (for some reason, but i do not see the variable in the PHP script,, i only see the HEIGHT of 100 variable)
What is needed to be changed to create a proportionate resize of 400 x 300.?
Anybody have an idea?
Cheers
I have PHP script that resizes a JPG image (from an upload function)
However, the script that I have DOES NOT resize the image PROPORTIONANTLEY, (sorry for spelling ;) )
The script i have attached resizes the image to a height of 100 pixels. and it changes the width to 500.. (for some reason, but i do not see the variable in the PHP script,, i only see the HEIGHT of 100 variable)
What is needed to be changed to create a proportionate resize of 400 x 300.?
Anybody have an idea?
Cheers
<?php
// include the Database classes
require_once('classes/database_php4.php');
// escape quotes and apostrophes if magic_quotes_gpc off
if (!get_magic_quotes_gpc()) {
foreach($_POST as $key=>$value) {
$temp = addslashes($value);
$_POST[$key] = $temp;
}
}
$db = new Database('db704.perfora.net','dbo277201649','Ycd2sZaR','db277201649');
$numrows = 1;
// if more than 3 send error message //
if ($numrows > 2) {
$maxwishes = 'Personal Upload Limit Reached for Today... Sorry';
echo 'duplicate=y&message='.urlencode($maxwishes);
}
else {
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
$new_height = 100;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
srand(time());
$random = (rand()%999);
$random2 = (rand()%999);
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$newname = 'cmsnewsv2'.$random.$random2.'.jpg';
$_POST['new_name'] = $newname;
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$newname}" );
unlink($tmp_img, "{$pathToimages}{$fname}");
$tmp_img = "";
$img = "";
$info = "";
$fname = "";
}
}
// close the directory
closedir( $dir );
}
createThumbs("files/","files/thumbs/",500);
$sql = 'INSERT INTO cmsnewsv2 (story_title,story_body,story_date,story_postedby,story_photo,story_link,story_price)
VALUES ("'.$_POST['ttitle'].'","'.$_POST['story'].'","'.$_POST['date'].'","'.$_POST['author'].'","'.$_POST['new_name'].'","'.$_POST['uurl'].'","'.$_POST['pprice'].'")';
$result = $db->query($sql);
if ($result) {
$created = 'Thankyou!';
echo 'duplicate=n&message='.urlencode($created);
}
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Erbureth: Good catch. After I saw the references to PHP4, I never bothered to read or test the script!
Best to all, ~Ray
Best to all, ~Ray
ASKER
Hello, Thank you for the lightning fast responses!!
@Erbureth, i have commented that line out,, and i have checked the result;
I uploaded a 80 x 60 image... and it converted it to 500 x 375.
-------------------------- -
soo, somewhere it is resizing the width to 500. i do not know where, (is that what 'thumbWidth' is referring to? ) --- line 41 and 42.
i'm confused!
@Erbureth, i have commented that line out,, and i have checked the result;
I uploaded a 80 x 60 image... and it converted it to 500 x 375.
--------------------------
soo, somewhere it is resizing the width to 500. i do not know where, (is that what 'thumbWidth' is referring to? ) --- line 41 and 42.
i'm confused!
ASKER
... Line 21 also... refers to 'thumbWidth'
Read over the code I posted in lines 36 to 76. It tests for appropriate sizes and does not resize images that are already small enough. Hopefully the comments in that section will be helpful. Best, ~Ray
This is because at line 68, you are calling function createThumbs with last parameter 500
If you see the function prototype at line 21, you will see that the last parameter is that thumbWidth, which is desired width of all thumbnails.
If you see the function prototype at line 21, you will see that the last parameter is that thumbWidth, which is desired width of all thumbnails.
Open in new window