Jump to content

Create thumbnail on upload


robyngae

Recommended Posts

I have created a page that uploads images to a directory but I need to be able to make thumbnails and have these stored in a seperate directory. I have been looking for three weeks through every tutorial that I can find and have downloaded and copied about 20 scripts but none work. I am obviously doing something wrong and my brain has now gone into information overload. Can someone please help me with a script that will upload the images to graphics and thumbnails to thumbs. Robyn

Link to comment
Share on other sites

Me too. I am also looking around for the script that would able to let me reduce the size of the image when the image has upload. I have come across so many script that require me to install the filter to my server. Check this web site Resize the Image.Unfortunately, my hosting provider disallow me to install the filter in my server. Therefore, I need a purely php script that could resize the uploaded image.

Link to comment
Share on other sites

You should try studying PHP and try to make the script yourself. We're not really here to do work for people, we're here to help teach.Here's the reference for imagecopyresampled(): http://php.net/imagecopyresampledIf you don't know PHP, w3schools has a good tutorial to get started. http://php.net has more advanced things that you may need.

Link to comment
Share on other sites

You should try studying PHP and try to make the script yourself. We're not really here to do work for people, we're here to help teach.Here's the reference for imagecopyresampled(): http://php.net/imagecopyresampledIf you don't know PHP, w3schools has a good tutorial to get started. http://php.net has more advanced things that you may need.
Thank you for your reply, you are right of course. I guess I was looking for a quick fix, I am moving in 2 weeks and it will be about 6 weeks before a satellite is installed for internet so I was rushing to finish the project as I hate having things incomplete.I have been reading the tutorials and I have thought for a while that the course I am doing is more concerned with us getting the codes than writing them ourselves. I will get back in and do some more study.Robyn
Link to comment
Share on other sites

  • 2 months later...
There are a couple samples on the imagecopyresampled reference page that will either resize an image to half it's original size, or resize it to a max of 200px proportionally, those should get you started.
Thank you I was ofline for a couple of months while I shifted house twice.I couldn't get anything on the reference page to work and have been working on my code. I have managed to make thumbnails on upload except for the png, the code for which I had to add in, can you tell me where I have gone wrong.<?php$add="graphics/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name. if(move_uploaded_file ($userfile, $add)){echo "Successfully uploaded the image";chmod("$add",0777);}else{echo "Failed to upload file Contact Site admin to fix the problem";exit;}///////// Start the thumbnail generation//////////////$n_width=100; // Fix the width of the thumb nail images$n_height=100; // Fix the height of the thumb nail imaage$tsrc="thumbs/$userfile_name"; // Path where thumb nail image will be storedecho $tsrc;if (!($userfile_type !=="image/JPEG" OR $userfile_type!=="image/gif" OR $userfile_type!=="image/png")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";exit;}/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////if (@$userfile_type=="image/gif"){$im=ImageCreateFromGIF($add);$width=ImageSx($im); // Original picture width is stored$height=ImageSy($im); // Original picture height is stored$newimage=imagecreatetruecolor($n_width,$n_height);imageCopySampled($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);if (function_exists("imagegif")) {Header("Content-type: image/gif");ImageGIF($newimage,$tsrc);}elseif (function_exists("imagejpeg")) {Header("Content-type: image/jpeg");ImageJPEG($newimage,$tsrc);}chmod("$tsrc",0777);}////////// end of gif file thumb nail creation//////////////////////// starting of JPG thumb nail creation//////////if($userfile_type=="image/pjpeg"){$im=ImageCreateFromJPEG($add); $width=ImageSx($im); // Original picture width is stored$height=ImageSy($im); // Original picture height is stored$newimage=imagecreatetruecolor($n_width,$n_height); imageCopySampled($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);ImageJpeg($newimage,$tsrc);chmod("$tsrc",0777);}//////////////// End of JPG thumb nail creation //////////////////////// starting of PNG thumb nail creation//////////if($userfile_type=="image/png"){$im=ImageCreateFromPNG($add); $width=ImageSx($im); // Original picture width is stored$height=ImageSy($im); // Original picture height is stored$newimage=imagecreatetruecolor($n_width,$n_height); imageCopySampled($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);ImagePng($newimage,$tsrc);elseif (function_exists("imagepng")) {Header("Content-type: image/png");ImagePNG($newimage,$tsrc);}chmod("$tsrc",0777);}//////////////// End of PNG thumb nail creation //////////?>ThanksRobyn
Link to comment
Share on other sites

whats the point of this part?ImagePng($newimage,$tsrc);elseif (function_exists("imagepng")) {Header("Content-type: image/png");ImagePNG($newimage,$tsrc);}first of all the elseif doesnt make sense cause there isnt an if before it (or at least not one that closes right before it)2nd of all u first use imagepng(), and then check if imagepng() exists, and then use it again

Link to comment
Share on other sites

whats the point of this part?ImagePng($newimage,$tsrc);elseif (function_exists("imagepng")) {Header("Content-type: image/png");ImagePNG($newimage,$tsrc);}first of all the elseif doesnt make sense cause there isnt an if before it (or at least not one that closes right before it)2nd of all u first use imagepng(), and then check if imagepng() exists, and then use it again
The tutorial for the first part of the code just said to copy and paste for png I think I got ythe elseif from the first part of the code.Robyn
Link to comment
Share on other sites

  • 2 weeks later...

You can use this:

<?phpfunction resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;    if ($src == '')  {	return;  }  if ($w == 0 && $h == 0)  {	return;  }  if ($dest == '')	$dest = $src; // resize in place  // open the image  $ext = strtolower(array_pop(explode('.', $src)));  switch ($ext)  {	case 'jpg':	case 'jpeg':	  $i = imagecreatefromjpeg($src);	break;	case 'gif':	  $i = imagecreatefromgif($src);	break;	case 'png':	  $i = imagecreatefrompng($src);	break;	default:	  return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w < $w && $h == 0) ||	  ($w == 0 && $h != 0 && $new_h < $h) ||	  ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h))  {	// image is small enough	if ($dest != $src)	  copy($src, $dest);	return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {	$new_h = ($w / $new_w) * $new_h;	$new_w = $w;  }  if ($h != 0 && $new_h > $h)  {	$new_w = ($h / $new_h) * $new_w;	$new_h = $h;  }    // resize  $new = imagecreatetruecolor($new_w, $new_h);  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {	case 'jpg':	case 'jpeg':	  imagejpeg($new, $dest);	break;	case 'gif':	  imagegif($new, $dest);	break;	case 'png':	  imagepng($new, $dest);	break;  }  imagedestroy($new);}?>

ex:

resize_image(array(  'source' => '/path/to/image.jpg',   'w' => 300)); // resize to max 300 px wide, overwrite originalresize_image(array(  'source' => '/path/to/source.jpg',   'dest' => '/path/to/thumb.jpg',   'w' => 150,   'h' => 150)); // resize to max 150x150, save as new file

This does require your server to support the GD image library. If the GD extension for PHP is not loaded, then you won't be able to resize images, no matter which script you use.

Link to comment
Share on other sites

  • 3 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...