Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Image upload and create tumbnail

    Hi, just found a useful script that im looking to modify for uploading and creating a thumbnail of an image.

    Code:
    $size = 80; // the thumbnail height
    
    	$filedir = 'pics/'; // the directory for the original image
    	$thumbdir = 'pics/'; // the directory for the thumbnail image
    	$prefix = 'small_'; // the prefix to be added to the original name
    
    	$maxfile = '2000000';
    	$mode = '0666';
    	
    	$userfile_name = $_FILES['image']['name'];
    	$userfile_tmp = $_FILES['image']['tmp_name'];
    	$userfile_size = $_FILES['image']['size'];
    	$userfile_type = $_FILES['image']['type'];
    	
    	if (isset($_FILES['image']['name'])) 
    	{
    		$prod_img = $filedir.$userfile_name;
    
    		$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
    		move_uploaded_file($userfile_tmp, $prod_img);
    		chmod ($prod_img, octdec($mode));
    		
    		$sizes = getimagesize($prod_img);
    
    		$aspect_ratio = $sizes[1]/$sizes[0]; 
    
    		if ($sizes[1] <= $size)
    		{
    			$new_width = $sizes[0];
    			$new_height = $sizes[1];
    		}else{
    			$new_height = $size;
    			$new_width = abs($new_height/$aspect_ratio);
    		}
    
    		$destimg=ImageCreateTrueColor($new_width,$new_height)
    			or $error['image'] = 'Problem in creating image'; $imageerror = 'regerrorwrong';
    		$srcimg=ImageCreateFromJPEG($prod_img)
    			or $error['image'] = 'Problem in opening source'; $imageerror = 'regerrorwrong';
    		if(function_exists('imagecopyresampled'))
    		{
    			imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
    			or $error['image'] = 'Problem in resizing'; $imageerror = 'regerrorwrong';
    		}else{
    			Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
    			or $error['image'] = 'Problem in resizing'; $imageerror = 'regerrorwrong';
    		}
    		ImageJPEG($destimg,$prod_img_thumb,90)
    			or $error['image'] = 'Problem in saving'; $imageerror = 'regerrorwrong';
    		imagedestroy($destimg);
    	}
    I have the code working now uploading the path of thumbnail and path of image to my databse, although now am looking to modify the creating thumbnail code a bit. I am looking basically to know if there is a simpler way of doing it than above, and also how to incororate gifs and maybe png files but produce error on other file types. At the moment if i try to upload for example a txt file it produces a lot of warning error messages:

    Sorry Guys, managed to get this working, after all. Just had a brainwave i guess.
    Last edited by cozzy1984; 02-14-2008 at 08:41 PM.


 

Posting Permissions

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