Jump to content

Image resize using GD


mfaulkner

Recommended Posts

Hello could somebody please help me, I'm sure its an easy request.

All I want to do is modify the code below to resize the uploaded image to certain dimensions, say '600 x 400'.

I'm thinking the code insert needs to go after the line:

[B]copy($_FILES['image_filename']['tmp_name'], $ImageName);[/B]

[code=php:0]<?php

// temporarily overides the memory_limt setting in php.ini
ini_set("memory_limit","200M");

// make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

// upload image and check for image type
$ImageDir = "/Library/WebServer/Documents/image-gallery/images/";
$ImageThumb = $ImageDir . "thumbs/";
$ImageName = $ImageDir . $image_tempname;


copy($_FILES['image_filename']['tmp_name'], $ImageName);


// get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);

if ($type > 3) {
echo "The only files that can be uploaded are GIF, JPG, or PNG.<br>";
echo "Please use the BACK button and try again";
} else {
// image is acceptable; ok to proceed


// insert info into table

$insert = "INSERT INTO images
(image_caption, image_username, image_date)
VALUES
('$image_caption', '$image_username', '$today')";

$insertresults = mysql_query($insert)
or die(mysql_error());

$lastpicid = mysql_insert_id();

$newfilename = $ImageDir . $lastpicid . ".jpg";

if ($type == 2)
{
rename($ImageName, $newfilename);
}

else
{
if ($type == 1)
{
$image_old = imagecreatefromgif($ImageName);

elseif ($type == 3)
{
$image_old = imagecreatefrompng($ImageName);
}

// "convert" the image to jpg
$image_jpg = imagecreatetruecolor($width, $height);
imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($image_jpg, $newfilename);
imagedestroy($image_old);
imagedestroy($image_jpg);
}

$newthumbname = $ImageThumb . $lastpicid . ".jpg";

// get dimensions for the thumbnail
$thumb_width = 150;
$thumb_height = 100;

// create the thumbnail
$largeimage = imagecreatefromjpeg($newfilename);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagejpeg($thumb, $newthumbname);
imagedestroy($largeimage);
imagedestroy($thumb);

$url = "location: showimage.php?id=" . $lastpicid;
header($url);

}


?>


[/code]


Thanks in advance, any help or suggestions would be very much appreciated.

Mark
Link to comment
https://forums.phpfreaks.com/topic/35511-image-resize-using-gd/
Share on other sites

you probably need to put the code in front of the copy line. Here's a starting point:

[code]<?php
$im=imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name']));
$width=600; //max width
$height=400; //max height
if(imagesx($im)>imagesy($im)) { //if landscape
if(imagesx($im)<=$width) { //if width is less than or equal to allowed
$newim=imagecreatetruecolor(imagesx($im),imagesy($im));
imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
} else { //width is larger than allowed
$newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$width));
imagecopyresampled($newim,$im,0,0,0,0,$width,((imagesy($im)/imagesx($im))*$width),imagesx($im),imagesy($im));
}
} else { //image is portrait
if(imagesy($im)<=$height) { //if height is less than or equal to allowed
$newim=imagecreatetruecolor(imagesx($im),imagesy($im));
imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
} else { //height is larger than allowed
$newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$height),$height);
imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$height),$height,imagesx($im),imagesy($im));
}
}
imagejpeg($newim,$_FILES[key($_FILES)]['tmp_name'],90); //hopefully replace original upload
if($im) imagedestroy($im);
if($newim) imagedestroy($newim);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/35511-image-resize-using-gd/#findComment-168267
Share on other sites

if you're trying to determine what filetype was uploaded, you can try this (keep in mind, I haven't tested it).

Since imagecreatefromstring will attempt to load numerous formats of images, much of your code will no longer be used. Here's a quick clean-up.

Just add the @ symbol to the first line like so:
[code]<?php $im=@imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name'])); ?>[/code]
You can then remove the getimagesize() line, and replace the [code=php:0]if($type>3)[/code] with the following:
[code]<?php
if(!$im) {
echo "The only files that can be uploaded are GIF, JPG, or PNG.<br>";
echo "Please use the BACK button and try again";
} else { //the rest of the script
?>[/code]

Finally, the [code=php:0]if($type==2)[/code] and it's respective else statement are fluff and can be eliminated.
Link to comment
https://forums.phpfreaks.com/topic/35511-image-resize-using-gd/#findComment-169005
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.