Page 1 of 2
Automatically resizing uploaded images
Posted: Mon Aug 23, 2004 11:35 am
by Burrito
I have a page set up for people to upload screen shots. I need to resize the images to 640x480. Ideally I'd be able to create another file for a thumbnail image as well at 100x75, but one step at a time here...
most of the images will probably be uploaded at 1280x1024 so the conversion should be pretty easy to do, I just don't know how. I need to resize the images primarily to cut down on file size but also to fit the pop up screen in which they will be shown.
any guidance you can provide will be greatly appreciated.
thx,
Burr
Posted: Mon Aug 23, 2004 11:47 am
by anjanesh
Image Functions :
Example for a jpg file :
Code: Select all
<?php
header("Content-type: image/jpeg");
$img=imagecreatefromjpeg($filename);
$width=imagesx($img);$height=imagesy($img);
$thumb_Width=85,$thumb_Height=85
$scale = min($thumb_Width/$width, $thumb_Height/$height);
$new_width=floor($scale*$width);$new_height=floor($scale*$height);
$tmp_img=imagecreatetruecolor($new_width,$new_height);
imagecopyresized($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
imagedestroy($img);
imagejpeg($tmp_img);
?>
Posted: Mon Aug 23, 2004 12:16 pm
by Burrito
Thanks for the response.
I'm getting an error however, it's not recognizing the function: imagecreatefromjpeg()
I'm using PHP 4.3.x, is that function not avail in the version that I'm using?
thanks again,
Burr
Posted: Mon Aug 23, 2004 12:25 pm
by anjanesh
Are you not having Image Library enabled ?
Change ;extension=php_gd2.dll to extension=php_gd2.dll in php.ini
Remove ;
Posted: Mon Aug 23, 2004 12:31 pm
by lostboy
http://www.hotscripts.com/PHP/Scripts_a ... index.html has a huge selection of prebuilt and tested functions
Posted: Mon Aug 23, 2004 12:39 pm
by Burrito
Anjanesh
I appreciate your time here, but I'm still having issues. I removed the ";" from the beginning of the line that said "extension=php_gd2.dll" and it locked up the php on the server (I couldn't hit any php pages on the server). For obvious reasons I"m leary about doing that again as this is a production server. Is there something else I need to "enable" to make this work properly? Do I need to restart php somehow after making the change?
Sorry about these mundane questions, but I'm a semi-noob with php.
Thx,
Burr
Posted: Mon Aug 23, 2004 12:42 pm
by lostboy
How about some info on the environment (OS, webserver)?
for windoze
copy the dll to the c:\windows\ folder to activate it, then restart the machine
for linux/apache
stop the webserver
uncomment the line in the .ini file
restart the webserver
Posted: Mon Aug 23, 2004 12:46 pm
by feyd
don't need to restart the machine.. just restart the service the web server is run under..
Posted: Mon Aug 23, 2004 12:46 pm
by anjanesh
Why don't you first experiment on a local machine.
For Windows nothing more is required except to re-start IIS/apache.
I dont know the method for linux but I think you'll have to compile it with some gd paramter. But I thought all versions of bundled PHP 4.3.x comes with gd enabled by default.
Most hosts have almost all extensions enabled and working.
Posted: Mon Aug 23, 2004 12:47 pm
by Burrito
yeah that might help, sorry.
it's doze 2k running IIS 5.0.
Incedentally I did a search for php_gd2.dll on the HD and came up blank.
where can I find the DLL?
thx again,
Burr
Posted: Mon Aug 23, 2004 12:49 pm
by lostboy
was this install from php, the windows installer? if so, then its not complete (ie it did not and does not include all the dlls) You will need to download the full zip file and take the needed dlls from there to place into the c:\windows folder...
Posted: Mon Aug 23, 2004 12:50 pm
by anjanesh
So you must have downloaded the binary installer php install which doesnt come with the libraries.
Download the zip file :
http://www.php.net/downloads.php
By the way PHP 4.3.9 RC 1 is out !
Posted: Mon Aug 23, 2004 12:52 pm
by anjanesh
lostboy wrote:....and take the needed dlls from there to place into the c:\windows folder...
Thats doesnt seem to be required as long as extension_dir = "c:\PHP\extensions" or whereever the extension dlls are.
Posted: Mon Aug 23, 2004 1:21 pm
by lostboy
maybe, but since he used the windows installer, that directive is not likely to be set or set correctly...
Posted: Mon Aug 23, 2004 3:05 pm
by Burrito
Holy smokes! I have finally gotten to the point where I can start testing the code you provided and I have it working great using
Code: Select all
<?php
header("Content-type: image/jpeg");
$img=imagecreatefromjpeg("images/testshot.jpg");
$width=imagesx($img);$height=imagesy($img);
$thumb_Width=640;
$thumb_Height=480;
$scale = min($thumb_Width/$width, $thumb_Height/$height);
$new_width=floor($scale*$width);$new_height=floor($scale*$height);
$tmp_img=imagecreatetruecolor($new_width,$new_height);
imagecopyresized($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
imagedestroy($img);
imagejpeg($tmp_img);
?>
Almost exactly like you posted it. However, I am having difficulty in getting it to work on my action page when I try to use a dynamic file name.
I guess I should have made all of this more clear in my original post, but I didn't think it would be this hard honestly. What I need to do is have a form page where people can select a file to upload using <input type="file">. Then when they upload it (it will be an image) it will resize the image and then dump the image into a database (blob field).
I'm definitaly getting closer thanks to your help, but I just can't seem to figure out how to give the name of the image from the uploaded form page and then how to save that image into the database.
here's what I had before that was working fine (inserting the large image to the db).
Code: Select all
<?
require_once("application.php");
if (isset($_FILES['attachfile']['tmp_name']) && !empty($_FILES['attachfile']['tmp_name']) && file_exists($_FILES['attachfile']['tmp_name']))
{
$name = $_FILES['attachfile']['name'];
$temper = explode('.',$name);
$ext = array_pop($temper);
if(($ext == "jpg") || ($ext == "JPG")){
//add attachment to this note
$size = filesize($_FILES['attachfile']['tmp_name']);
$size = $size ? $size : 'NULL';
$insertattachment = "INSERT INTO screenshots ( matchid, todaysdate, ipadd, filesize, filename ) VALUES (".$_GET["id"].", now(), '" . mysql_escape_string($_SERVER['REMOTE_ADDR']) . "', {$size}, '".$name."' )";
mysql_query($insertattachment)
or trigger_error("Couldn't insert attachment.".mysql_error(), E_USER_ERROR);
$attachid = mysql_insert_id();
$attachhandle = fopen($_FILES['attachfile']['tmp_name'], 'rb');
while (!feof($attachhandle))
{
$insertattachpiece = "INSERT INTO screenshotsdata ( screenshotid, filedata ) VALUES ( {$attachid}, '" . mysql_escape_string(fread($attachhandle, 65535)) ."' )";
mysql_query($insertattachpiece)
or trigger_error("Couldn't insert attachment piece.".mysql_error(), E_USER_ERROR);
} // end while attachment data is there
$message = "Screenshot Uploaded Successfully<br><a href="javascript:uploadMore()" class="bottom">Upload Another Screenshot For This Match</a>";
}else{
$message = "<span class="red">Could Not Upload File, Not A ".jpg" or ".JPG" Extension</span><br><a href="javascript:history.go(-1)" class="bottom">Click Here To Go Back And Try Again</a>";
} // end if for file name check
} else {
$message = "<span class="red">Could Not Upload File, Unknown Error</span><a href="javascript:history.go(-1)" class="bottom">Click Here To Go Back And Try Again</a>";
} // end if for file data check
?>
what I'm missing is probably due in large part to my being such a noob, so I apologize for my ignorance.
thanks again everyone,
Burr