SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Duplicate and Resize Image During Upload

    Hi all. I have this code for uploading an image, not for anything particular at the moment I'm just trying to learn things.

    Basically at the moment this code uploads a file successfully if it is the right file type and applies an MD5 hash filename to it (not for any real reason, I have a method to just remove unsavoury characters instead but the MD5 was the last step I used and I haven't undone it.

    Anyway, what I am after is a tutorial on creating a function or just series of code to add to the script below that will create a second, resized copy of whatever image I upload and append '_thumb' to it. The name part isn't hard but I am struggling to find a tutorial that has decent description of what is going on, that I can apply to what I currently have or that isn't just a script to download.

    I'm more interested in a tutorial so I can understand what is happening as I move through the script. If anyone can provide such a tutorial I'd be much appreciative.

    P.S. I would like to do this purely in Server Side if possible and avoid javascript.

    Code I have so far:
    Code PHP:
    <?php
     
    function check_file($file) {
    	$valid_types = array("image/pjpeg","image/jpg","image/jpeg","image/gif","image/png");
     
    	if(in_array($file['type'],$valid_types)) 
    		return 1;
    		return 0;
    }
    $special = array('/','!','&','*',' ','-');
     
    define('TARGET_PATH', 'uploadimages/');
    $image = $_FILES['image'];
    $image['name'] = stripslashes($image['name']);
    $file_basename = substr($image['name'], 0, strripos($image['name'], '.')); // strip extention
    $file_ext = substr($image['name'], strripos($image['name'], '.')); // strip name
    $image['name'] = md5($file_basename).$file_ext;
     
    if(check_file($image)){
     
    	if ($image['error'] > 0)
    		{
    		echo "Error: " . $image["error"] . "<br/>";
    		}
    	else
    		{
    		if (file_exists(TARGET_PATH . $image["name"]))
    			{
    			echo $image["name"] . " already exists.";
    			}
    		else
    			{
    			move_uploaded_file($image["tmp_name"], TARGET_PATH. $image["name"]);
    			echo "Stored in: " . TARGET_PATH . $image["name"] . "<br />";
    			echo "Upload " . $image["name"] . "<br />";
    			echo "Type " . $image["type"] . "<br />";
    			echo "Size " . $image["size"] / 1024 . "Kb <br />";
    			echo "<img src=" . TARGET_PATH . $image['name'] ." />";
    			}
    		}
     
    	}
    else
    	{
    	echo "Invalid file";
    	}
    ?>

  2. #2
    SitePoint Zealot romance's Avatar
    Join Date
    Apr 2004
    Location
    UK
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this goes against what you asked for, But IMHO things such as resizing an image are so trivial nowadays that there's not a whole lot of point trying to write your own resizing script/class when there's a lot of very decent solutions already out there.

    I like http://phpthumb.gxdlabs.com/ which has a class for creating new thumbs, applying different resize methods etc. all with a few lines of code.

    By looking at the code of classes like this, you'll see other techniques such as method chaining, exceptions etc which will serve you in the long run to becoming a better developer, rather than simply knowing how to resize an image. If the script is too hard to follow, you could look at getting a debugger to step through the code, always helps me.

  3. #3
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, using an external library is most likely the way to go, so you don't have to bother with special cases and possible bugs (while the math for resizing is easy, it isn't trivial).

    That being said, the basic code looks something along this:
    PHP Code:
    // load the image
    $im imagecreatefromjpeg('big image.jpg');

    // get width and height
    $width imagesx($img);
    $height imagesy($img);

    // calculate the ratio
    $ratio $width $height;
    // do some calculations with it
    // explained in more detail on http://stackoverflow.com/questions/1729887/little-math-help-for-image-resize-needed

    // resize the image to $new_width, $new_height
    $new_img imagecopyresampled($img, ...); // better quality, works fine with alpha channel
    $new_img imagecopyresized($img, ...); // not so smooth, appropriate for 8-bit transparent gifs only

    // save to a file
    imagejpeg($new_img'resized.jpg'); 

    In contrast, all the code you need, if you use WideImage, is:
    PHP Code:
    include 'WideImage/WideImage.php';
     
    WideImage::load('image')->resize(200200)->
      
    saveToFile(TARGET_PATH $file_basename '_thumb' $file_ext); 

  4. #4
    SitePoint Member webnathan's Avatar
    Join Date
    Jul 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all,

    I am also looking for a script that will allow you to upload a image (any image type) jpg,gif,png etc and create a thumbnail as well.

    the script needs to store the thumbnails in the thumbnails folder and the original image in the images dir.

    I am a newbie and have searched google and tried some samples ,but all i found was image galleries.

    I want to create my own image gallery ,but i just need the script for the above.
    if the script inserts the images into a database it is also or not.

    Thanks in advance.

    Nathan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •