Jump to content

Image upload and create tumbnail


cozzy1984

Recommended Posts

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

 

$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:

 

Warning: Division by zero in C:\wamp\www\Adll Do\editaddvert.php on line 169

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\Adll Do\editaddvert.php on line 180

 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in C:\wamp\www\Adll Do\editaddvert.php on line 182

 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'pics/wireless.txt' is not a valid JPEG file in C:\wamp\www\Adll Do\editaddvert.php on line 182

 

Warning: imagesx(): supplied argument is not a valid Image resource in C:\wamp\www\Adll Do\editaddvert.php on line 186

 

Warning: imagesy(): supplied argument is not a valid Image resource in C:\wamp\www\Adll Do\editaddvert.php on line 186

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\Adll Do\editaddvert.php on line 186

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\Adll Do\editaddvert.php on line 192

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\Adll Do\editaddvert.php on line 194

Link to comment
https://forums.phpfreaks.com/topic/91130-image-upload-and-create-tumbnail/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.