Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving images

Status
Not open for further replies.

sandravega

Programmer
Feb 10, 2004
43
AR
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
 
hmmm I would do it with the $_FILE variable.

then you can use somethng like this where imagefile = the name of my filebox in my form.

if ($_FILES['img']['size'] <= 2000000)
{
copy ($_FILES['img']['tmp_name'], "./img/".$_FILES['img']['name']) or die ("Could not copy");
}
else
echo "Het bestand is te groot. Maximale bestandsgrote is 2 megabytes.";

except tmp_name, size and name you also get the attribute type
$_FILES['img']['type']

this one tells you if it is a jpg our a gif or whatever.

best regards.


tank you,

(>" "<)
(='o'=)
-(,,)-(,,)-----
|LORD_GARFIELD|
---------------
 
Thanks Lord Garfield
The $files wasn't the solution since it allows me to copy but not to resize...
the answer was so simple,
I was using fwrite to write and I should use "imageJpeg"
so the right code was:

$src=$HTTP_POST_VARS['imagen'];
//$im=fopen($src, "rb");
$data = fread(fopen($src, "rb"), filesize($src));
//$data = addslashes(fread(fopen($src, "rb"), filesize($src)));
$im = imagecreatefromstring($data);
$width = imagesx($im);
$height = imagesy($im);

// Set thumbnail-width to 100 pixel
$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
$nombre = "nuevo";
$nmini = "nuevo_c.jpg";
$ncom = "nuevo_g.jpg";
imagejpeg($thumb,$nmini,75);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top