Jump to content

[SOLVED] Image Resize - I need to create a Small, Medium, and Large image


Recommended Posts

Hello Guys,

 

I'm working on a project and I need to upload an image then resize it, I have the project working right now with the following code but the problem is it only resizes the thumbnails, I need it to resize a 1024px image down to 640px wide and then make a 360px image and a 222px image:

	$size = 150; // the thumbnail height

$filedir = '../images/stories/property/'; // the directory for the original image
$thumbdir = '../images/stories/property/thumbnails/'; // the directory for the thumbnail image

$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.$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 die('Problem In Creating image');
	$srcimg=ImageCreateFromJPEG($prod_img)
		or die('Problem In opening Source Image');
	if(function_exists('imagecopyresampled'))
	{
		imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}else{
		Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}
	ImageJPEG($destimg,$prod_img_thumb,90)
		or die('Problem In saving');
	imagedestroy($destimg);
}

 

But I would like to make 3 images when uploading, a Large, Medium, and Small image. I have folders setup for each of them in the images folder, I want the large images to go into the ../images/large/ and the medium size images to go in ../images/ and the small images to go in ../images/thumbnails/

 

This is the code that I was trying to use as it was a bit easir to understand, but it was not working out for me.

// Large Image
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=640;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$filename = "../images/stories/property/large/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// Normal Image
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=360;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$filename = "../images/stories/property/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// Thumbnail Image
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=222;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$filename = "../images/stories/property/thumbnails/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);

well I found part of the problem that I was having was in my code:

$_FILES['uploadfile']

It needed to be:

$_FILES['image']

 

Ok now on to aspect ratios

 

This is the code that I'm using now:

// Large Image
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=640;
$newheight=($height/$width);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$filename = "../images/stories/property/large/". $_FILES['image']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// Normal Image
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=360;
$newheight=($height/$width);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$filename = "../images/stories/property/". $_FILES['image']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// Thumbnail Image
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=222;
$newheight=($height/$width);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$filename = "../images/stories/property/thumbnails/". $_FILES['image']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);

 

and these are the errors that I'm getting:

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 11

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 12

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 14

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 16

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 23

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 24

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 26

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 28

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 35

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 36

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 38

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\First Commercial Real Estate\administrator\components\com_property\update_image_form.php on line 40

Ok I fixed the aspect ratio

$newwidth=450;
$newheight=($height/$width);

 

It needed to have what ever the new width is set to then set *450 to be the same, like so:

$newwidth=450;
$newheight=($height/$width)*450;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.