Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

imagecopyresampled images are too big

Status
Not open for further replies.

Haraldo

Programmer
Jun 9, 2003
41
GB
I have an image upload that uploads an image to a tmp directory from there the image can be viewed. It is resampled at 50% and is fine.

Then when a client is happy they can submit the image which is then cut into segments. I take the tmp image and imagecopyresample it again - see below.


// Get tmp image
$image = $_SERVER['DOCUMENT_ROOT'].'/images/tmp/'.$_SESSION['username'].'_'.$_SESSION['uploadFile'];

// Find out what type of image file it is
$imgData = getimagesize( $image );
$type = $imgData[2];

// Gif = 1 JPEG = 2
if ( $type == 1 )
$advert = imagecreatefromgif( $image ) or die('Cannot Initialize new GD image stream for GIF');
else if ( $type == 2 )
$advert = imagecreatefromjpeg( $image ) or die('Cannot Initialize new GD image stream for JPEG');

$sliced_advert = imagecreatetruecolor( BLOCK_WIDTH, BLOCK_HEIGHT ); // creates the correct size

// Get image size
$image_width = imagesx( $advert );
$image_height = imagesy( $advert );

$width = $image_width / BLOCK_WIDTH; // should be 20 for v.1
$height = $image_height / BLOCK_HEIGHT; // should be 20 for v.1

$col = $row = $count = 0;

for ($y=0; $y<$height; $y++)
{
for ($x=0; $x<$width; $x++)
{
imagecopyresampled( $sliced_advert, $advert, 0, 0, $col, $row, BLOCK_WIDTH, BLOCK_HEIGHT, imagesx( $sliced_advert ), imagesy( $sliced_advert ) );

if ( $type == 1 )
{
imagegif( $sliced_advert, $_SERVER['DOCUMENT_ROOT'].$client_folder.'/image_'.$image_id.'/'.$_SESSION['username'].'_'.$count.'.gif', 50 );
$imageFile[$count] = $client_folder.'/image_'.$image_id.'/'.$_SESSION['username'].'_'.$count.'.gif';
}
if ( $type == 2 )
{
imagejpeg( $sliced_advert, $_SERVER['DOCUMENT_ROOT'].$client_folder.'/image_'.$image_id.'/'.$_SESSION['username'].'_'.$count.'.jpg', 50 );
$imageFile[$count] = $client_folder.'/image_'.$image_id.'/'.$_SESSION['username'].'_'.$count.'.jpg';
}
$count++;
$col += BLOCK_HEIGHT;
}
if( $image_width == $col )
{
$col = 0;
}
$row+=BLOCK_HEIGHT;
}
imagedestroy( $sliced_advert );

Now if you understood that lot which i hope you have!

I have an example, say the tmp image is 4,590 bytes in size. Once the actions above have taken place each segment created is about 800 bytes in size on average. If you add all the segments together it works out to be up to 9 times larger than the tmp image itself. Its extraordinary! If you add up the segments they come to 38,958 bytes. So my question is why and how can i fix this??

I presume its something to do with me not understanding how to create the images correctly. However it all works just the image segments are too large ( in bytes ).

Thanks for any help,
 
I don't know what BLOCK_WIDTH and BLOCK_HEIGHT are set to but the easy way is to do it using specialized math similar to this:
Code:
if ($image_width > $target_width) {
	$scale = $target_width/$image_width;
	$new_image_width = $image_width*$scale;
	$new_image_height = $image_height*$scale;
} else {
	$new_image_height = $image_height;
	$new_image_width = $image_width;
}
if ($new_image_height > $target_height) {
	$scale = $target_height/$new_image_height;
	$new_image_width = $new_image_width*$scale;
	$new_image_height = $new_image_height*$scale;
}
I use this code to scale images for thumbnails.
Code:
imagejpeg( $sliced_advert, $_SERVER['DOCUMENT_ROOT'].$client_folder.'/image_'.$image_id.'/'.$_SESSION['username'].'_'.$count.'.jpg', 50 );
                $imageFile[$count] = $client_folder.'/image_'.$image_id.'/'.$_SESSION['username'].'_'.$count.'.jpg';
This section of your code doesn't make it any smaller height/width wise, only size wise as the image will be less clear and crisp. The 50 in that statement merely changes the quality that the image is displayed with.
 
Thanks Matt,

BLOCK_WIDTH & HEIGHT are actually set to 20 and no scaling is actually necessary. The for loop goes around and cuts 20 x 20 segments from the original image which might be 40x40 for instance (going around 4 times therefore). I then reduce the quality of the image by 50 in the hope of reducing the image bytes size. However each time the segments are about 700 to 900 bytes in size. When the origninal image itself, in comparison is much smaller ( all the segments added together are much larger than the original ).

Also by this stage in my code the original image has already been resampled by 50% before we start. All i want to do really at this stage is store the image segments but they turn out soooo large in bytes it makes me think i'm doing the image creation bit wrong!

Thanks again,
 
This is another call out to anyone that can help me discover what i'm doing wrong with my imagecopy thats creating the segments i cut from the original picture being so large in bytes.

THANKS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top