sandravega
Programmer
Hi
I'm trying to accomplish this:
If I let the users upload images in my web, I want to get some control over the size of the images they´re uploading. Also, I want to save the images as separate files in a directory, not in a blob field in a database.
So my code gets the image from the path provided by the user, gets its data, reduces it and then tries to save in "nuevo_c.jpg" file.
The "nuevo_c.jpg" file appears where expected, but it's a 1kb .jpg and it contains no image, just black...
¿does anybody knows what am I doing wrong? ¿what's missing?
thanks
CODE
//gets the path given by the user in the form
$src=$HTTP_POST_VARS['imagen'];
//reads it
$data = fread(fopen($src, "rb"), filesize($src));
//creates a new image
$im = imagecreatefromstring($data);
//gets its size
$width = imagesx($im);
$height = imagesy($im);
// Set thumbnail-width to 80pixel
$imgw = 80;
// calculate thumbnail-height from given width to maintain aspect ratio
$imgh = $height / $width * $imgw;
// create new image using thumbnail-size
$thumb=ImageCreate($imgw,$imgh);
// copy original image to thumbnail
ImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
//write image to file
$nmini="nuevo_c.jpg";
$mini=fopen ($nmini, "wb");
fputs($mini, $thumb);
fclose($mini);
END OF CODE
I'm trying to accomplish this:
If I let the users upload images in my web, I want to get some control over the size of the images they´re uploading. Also, I want to save the images as separate files in a directory, not in a blob field in a database.
So my code gets the image from the path provided by the user, gets its data, reduces it and then tries to save in "nuevo_c.jpg" file.
The "nuevo_c.jpg" file appears where expected, but it's a 1kb .jpg and it contains no image, just black...
¿does anybody knows what am I doing wrong? ¿what's missing?
thanks
CODE
//gets the path given by the user in the form
$src=$HTTP_POST_VARS['imagen'];
//reads it
$data = fread(fopen($src, "rb"), filesize($src));
//creates a new image
$im = imagecreatefromstring($data);
//gets its size
$width = imagesx($im);
$height = imagesy($im);
// Set thumbnail-width to 80pixel
$imgw = 80;
// calculate thumbnail-height from given width to maintain aspect ratio
$imgh = $height / $width * $imgw;
// create new image using thumbnail-size
$thumb=ImageCreate($imgw,$imgh);
// copy original image to thumbnail
ImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
//write image to file
$nmini="nuevo_c.jpg";
$mini=fopen ($nmini, "wb");
fputs($mini, $thumb);
fclose($mini);
END OF CODE