View Full Version : upload and resize
lord22
11-12-2009, 01:49 PM
Hi,
I've created this function:
function resizeImage($originalImage){
list($width, $height) = getimagesize($originalImage);
$new_width = 54;
$new_height = 54;
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $imageResized;
}
that enables me to resize images to size of 54*54, however I don't know how to send the function the image with php, and upload it to the server.
Any help?
Thanks :)
djr33
11-12-2009, 05:44 PM
Look up any tutorial on uploading a file. There are many. In short you will use action="POST" for the form and a <input type="file"> to choose the image. Then submit that to your PHP script. After that it gets a little complex, but basically it will have a filename available to you (based on your settings), and you can do security things like only allow .jpg, etc (best to only allow certain types, rather than trying to block all bad types-- you won't be able to predict everything dangerous).
Then once that image is on your server, you can either save it to the server as a file (actually, move the temporary file) or use it as is. Either way you will supply the path and create a GD image using imagecreatefromJPEG() (or -GIF, -PNG, or even google for imagecreatefromBMP() which is not included by default).
That will return a GD image: $im = imagecreatefromJPEG().
Then you can call the function above with $im as the parameter.
Take each step slowly and separately and this should not be too difficult to work out.
After that you can save the resulting image as a file or display it directly, as you wish. Since it is immediately upon the upload, it is probably better to save it as a file and embed that new link into an html page so the user has some more feedback than just viewing an image.
lord22
11-12-2009, 09:20 PM
Thanks
Piotto
11-13-2009, 01:00 PM
hi lord22
this works for me...
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="new_image" id="new_image" size="30" type="file">
<button name="submit" type="submit">Upload/Resize Image</button>
</form>
<?php
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "./myphotos/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "./myphotos/" . $imagepath; //This is the new file you saving
$file = "./myphotos/" . $imagepath; //This is the original file
list($width, $height) = GetImageSize($file) ;
$modwidth = 500;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreate($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresized($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 500) ;
echo "Resized image:<br><br> <img src='./myphotos/".$imagepath."'>";
}
}
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.