Re: scaling images

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hulf wrote:
Sorry my message was cut off.

Yes I want to scale to 300 x 200px before I upload the image to my folder. Here is my code so far.

thanks,

H.


$imageinfo = getimagesize($_FILES['userfile']['tmp_name']);

echo $x=$imageinfo[0];
echo $y=$imageinfo[1];


$newwidth = 300;
$newheight = 200;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($_FILES['userfile']['tmp_name']);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
$myimage = imagejpeg($thumb);

$target_path = "../property_images/$property_id/".basename( $_FILES['userfile']['name']);

$img_url= $property_id."/".basename( $_FILES['userfile']['name']);

if(move_uploaded_file($myimage, $target_path)) {
  /*  echo "The file ".  basename( $_FILES['userfile']['name']).
    " has been uploaded";*/
} else{
    echo "There was an error uploading the file, please try again!";
}

After resizing the image you will have the newly created JPEG
data (binary) in the $myimage variable, not a file(name).

So you actually have to do something like:

file_put_contents($target_path,$myimage);

This will put the content in the $myimage variable ( the resized JPEG
data) into the target path. Note that you should also check if the
uploaded file is a supported image, so do something like :

if($source = @imagecreatefromjpeg($_FILES['userfile']['tmp_name']))
{
	// file is a supported image type, resize it and save it
}
else
{
	// unsupported image file or not an image file at all,
	// display some error here
}


Note:
file_put_contents is a PHP5 only function, use fopen,fwrite,fclose
for PHP4.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux