Jump to content

Recommended Posts

I'm working on an application that builds a listing in a directory. I'd like to add an image to the process so I've been working on the following simple test page. It works for uploading the file just fine as far as I can tell.

 

I obviously want to control the size of the uploaded image. It needs to go in a small thumbnail size space. I've loaded and enabled GD and the $array=gd_info () function looks like GD is working properly.

 

I've been reading the documentation and have been fooling with an example here:http://us3.php.net/manual/en/function.imagecopyresized.php

 

The function I need to use is imagecopyresized - I think - anyway there isn't any other functions having to do with size.

 

I'm having trouble though. Does anyone have experience in how to use this function? The example I have may be to complicated for you current understanding.

 

Here is the code I'm trying to add it to. I just want to take an image file from the user, resize it, then plop it on the server.

 

<?php
$file = (isset($_POST['file'])? $_POST['file'] : '');
$name = (isset($_POST['file'])? $_POST['file'] : '');

define ("FILEREPOSITORY", $_SERVER['DOCUMENT_ROOT'] . "/gentledoberman/builders/make_listing/images");

if (is_uploaded_file($_FILES['file']['tmp_name']))
{
if ($_FILES['file']['tmp_name'] == "image/gif")
{
	echo "wrong file type.";
}
else
{
	$name = (isset($_POST['name'])? $_POST['name'] : '');		
	$result = move_uploaded_file($_FILES['file']['tmp_name'], FILEREPOSITORY . "/$name.gif");
}
	if ($result == 1) echo "Success";
	else echo "Problem uploading";

echo "<img src='images/" . $name . ".gif' />";
}
?>
<form name='form1' id='form1' enctype='multipart/form-data'	
action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" >
<input type='file' name='file' value='<?php echo $file; ?>' /><br />
<input type='text' name='name' value='<?php echo $name; ?>' /><br />
<input type='submit' name='submit' value='submit' />
</form>

Is this code of yours or someone's else? It has some things which are overdone or not right. Why use is_uploaded_file() and move_uploaded_file() when they do exactly the same. The mime type of the file can be determined with $_FILES['file']['type'] or u can just get the extension:

 

$ext = strtolower(substr(strrchr($_FILES['file']['name'], '.'), 1);

 

Now to go to the real thing. The following code can make it for you:

 

<?php
$originalImage = imagecreatefromjpeg("path_of_the_original");
list($width, $height) = getimagesize("path_of_the_original");
if($width >= $height){
$ratio = $width/$height;
$newWidth = 100;
$newHeight = $newWidth/$ratio;
} else{
$ratio = $height/$width;
$newHeight = 100;
$newWidth = $newHeight/$ratio;
}
$tempImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tempImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($tempImage, "path_to_new_image/new_name.jpg", 80);
imagedestroy($originalImage);
imagedestroy($tempImage);
?>

 

The code actually gets the original image's width and height, calculates ratio and resizes the thumb to that ratio with a fixed width or height of 100 pixels. Then it creates the thumb by copying the original one with imagecopyresampled() (used it instead of imagecopyresized() to also maintain anti alias, so the image quality is a lot better). Finally it destroyes the image handlers. I didnt properly test the code so it may contain any error, but i guess it would do it. Just keep in mind that it must be used after u upload the file, and the functions used imagecreatefromjpeg() and imagejpeg() are for jpg file types. For gif and png use respectively imagecreatefrompng(), imagepng() and imagecreatefromgif(), imagegif(). Make some condition for the file type to determine the right function to use. Hope it helps and is what u were searching for.

Hey GuiltyGear,

 

things which are overdone or not right

 

You said it, sounds like my code. That's why I'm here....

 

I wrote it but I used an example in a book I have. From what I understand, according to the book, is_upload_file() check if a file was uploaded with the POST command, handy for protecting the input from use by malicious people.

 

move-upload-file() moves a file from the temp directory to the final location, AND checks that is comes via post, so is_upload_file() used before it is unnecessary. However, in my code, and in the example I've used, it is used to signal the code in the if statement.

 

As for the the file type, I'm not sure which is best. Using the $_FILES array seems like a good one though.

 

Thanks for the example!

 

Good piece of code GuiltyGear, thank.

Dont know why, but i thought it wasnt your code. Just an impression though  :)

 

With is_uploaded_file() u're checking if the temp file has been created and with move_uploaded_file() u actually upload the file from temp to the new location. If move_uploaded_file() returns true then it is uploaded so theres not need to overdo it with a is_uploaded_file().

 

About the file type its ok to use it like that, but u need $_FILES['file']['type'], as im not sure it will work the way u did it. Getting the extension is just an alternative.

 

Did u try the GD thumbnail thing? If u have problems, just tell.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.