Automatically resizing uploaded images
Moderator: General Moderators
Automatically resizing uploaded images
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
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
Image Functions :
Example for a jpg file :
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);
?>http://www.hotscripts.com/PHP/Scripts_a ... index.html has a huge selection of prebuilt and tested functions
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
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
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.
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.
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 !
Download the zip file : http://www.php.net/downloads.php
By the way PHP 4.3.9 RC 1 is out !
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
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).
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
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);
?>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
?>thanks again everyone,
Burr