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,
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,