Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to rotate and crop a transparent png?plz solve my problem
Old 08-01-2008, 03:18 AM how to rotate and crop a transparent png?plz solve my problem
Junior Talker

Posts: 4
Trades: 0
I cant crop and embose a transparent png on jpeg which is rotated.
when i try to crop the rotated transparent png its coming with some background color and loosing its transparency.How can i do that?
chinna_ann is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-01-2008, 04:10 AM Re: how to rotate and crop a transparent png?plz solve my problem
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
do we get code?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-01-2008, 04:49 AM Re: how to rotate and crop a transparent png?plz solve my problem
Junior Talker

Posts: 4
Trades: 0
This code is for rotatig transparent png without loosing its transparency

function createpngthumb($sourceImage,$new_width,$new_height ,$tp_index=2,$dest_image)
{
list($old_Width, $old_Height, $type, $attr) = getimagesize($sourceImage);

$im_arr= getimagesize($sourceImage);
$old_Width=$im_arr[0];
$old_Height=$im_arr[1];

$thumbHeight=$new_height;
$thumbWidth=$new_width;
#$image = imagecreatefrompng($sourceImage);
// Create the image based on image type


$image = imagecreatefrompng($sourceImage);




if ( ($old_Width>=$thumbWidth) or ($old_Height >= $thumbHeight))
{

if ($old_Width> $old_Height)
{
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $old_Height * ( $thumbWidth / $old_Width ) );

$left_pad = 0;
$top_pad = ($thumbHeight - $new_height) / 2;

}
else
{
// calculate thumbnail size
$new_height = $thumbHeight;
$new_width = floor( $old_Width * ( $thumbHeight / $old_Height ) );

$left_pad = ($thumbWidth - $new_width) / 2;
$top_pad = 0;
}

//echo 'new-width'.$new_width.'height'.$new_height;

$image_resized = imagecreatetruecolor( $thumbWidth, $thumbHeight );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

$trnprt_indx = imagecolortransparent($tmp_img);
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($tmp_img, $tp_index);
imagealphablending($tmp_img, false);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($tmp_img, 255, 255, 255);

// Completely fill the background of the new image with allocated color.
imagefill($tmp_img, 0, 0, $trnprt_indx);
imagesavealpha($tmp_img, true);
// Set the background color for new image to transparent
imagecolortransparent($tmp_img, $trnprt_indx);

// copy and resize old image into new image
imagecopyresampled( $tmp_img, $image, 0,0, 0, 0, $new_width, $new_height, $old_Width, $old_Height );

$trnprt_indx = imagecolortransparent($image_resized);
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($image_resized, $tp_index);
imagealphablending($tmp_img, false);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image_resized, 255, 255, 255);

// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $trnprt_indx);
imagesavealpha($tmp_img, true);
// Set the background color for new image to transparent
imagecolortransparent($image_resized, $trnprt_indx);

// imagecopyresampled($image_resized, $tmp_img, 0, 0, 0, 0, $new_width, $new_height, $new_width, $new_height);
imagecopyresampled($image_resized, $tmp_img, $left_pad, $top_pad, 0, 0, $new_width, $new_height, $new_width, $new_height);


}
else
{

// calculate thumbnail size
$new_height = $old_Height;
$new_width = $old_Width;

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

$trnprt_indx = imagecolortransparent($tmp_img);

// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($tmp_img, $tp_index);
imagealphablending($tmp_img, false);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($tmp_img, 255, 255, 255);

// Completely fill the background of the new image with allocated color.
imagefill($tmp_img, 0, 0, $trnprt_indx);
imagesavealpha($tmp_img, true);
// Set the background color for new image to transparent
imagecolortransparent($tmp_img, $trnprt_indx);
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $image, 0,0, 0, 0, $new_width, $new_height, $old_Width, $old_Height );


$left_pad = ($thumbWidth - $new_width) / 2;
$top_pad = ($thumbHeight - $new_height) / 2;

// create empty thumbnail img
$image_resized = imagecreatetruecolor($thumbWidth, $thumbHeight);

$trnprt_indx = imagecolortransparent($image_resized);
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($image_resized, $tp_index);
imagealphablending($tmp_img, false);
$text_color = imagecolorallocate($image_resized, 255, 255, 255);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $trnprt_indx);
imagesavealpha($tmp_img, true);
// Set the background color for new image to transparent
imagecolortransparent($image_resized, $trnprt_indx);



imagecopyresampled($image_resized, $tmp_img, $left_pad, $top_pad, 0,0, $new_width, $new_height ,$new_width, $new_height );

}
//create


$rotate = imagerotate($image_resized, 180, 0) ;

$im_new = imagepng($rotate,$dest_image,9);


#SET THE PERMISSIONS TO THE FOLDER
//chmod($dest_image,0777);
}

it ouputs some image

And now this code is to crop that image
function crop_image($nw, $nh, $source, $dest)
{
$size = getimagesize($source);

$w = $size[0];

$h = $size[1];

//extracting the extension of the given file
$stype= $this->file_extension($source);

//getting pathinformation
$path_info= $this->pathinfo_image($source);

//getting filename without extension
$filename= $path_info['basename'];

switch($stype)
{
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
case 'jpeg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}

$dimg = imagecreatetruecolor($nw, $nh);

$wm = $w/$nw;

$hm = $h/$nh;

$h_height = $nh/2;

$w_height = $nw/2;

if($w> $h)
{
$adjusted_width = $w / $hm;

$half_width = $adjusted_width / 2;

$int_width = $half_width - $w_height;

imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
}
elseif(($w <$h) || ($w == $h))
{
$adjusted_height = $h / $wm;

$half_height = $adjusted_height / 2;

$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
}
else
{
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w, $h);
}

//imagejpeg($dimg, $dest.$filename,100);
//create
switch($stype)
{
case 1 : //Image is type gif.
$im_new = imagegif($th_img,$dest);
break;
case 2 : //Image is type jpeg.
$im_new = imagejpeg($dimg, $dest,80);
break;
case 3 : //Image is png.
$im_new = imagepng($dimg, $dest,9);
break;
default :
$im_new = imagejpeg($dimg, $dest,80);
break;
}

}
I am success to rotate image without losing its transparency .

And after that if it was cropped, it is not working
chinna_ann is offline
Reply With Quote
View Public Profile
 
Old 08-01-2008, 07:38 AM Re: how to rotate and crop a transparent png?plz solve my problem
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I think the answer is simple.
Jpg cannot have a transparent layer nor color.

gif can have 1 color defined as transparent, and png can have an alpha channel that indicate how much transparency is set.

So, simple answer is: you cannot keep transparency when you convert an png image to a jpg.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-01-2008, 08:42 AM Re: how to rotate and crop a transparent png?plz solve my problem
Junior Talker

Posts: 4
Trades: 0
Thank u for response.
Actually i am successfully embosed png image on jpeg.
but i cant crop the image if it was rotaed. There is the problem was coming.
i didnt get what is the problem with rotation.With out rotaion i can embose image on jpeg.

The order was.

1.rotate image
2.crop image
3.embose on jpeg.

Once again thanks for responding.
chinna_ann is offline
Reply With Quote
View Public Profile
 
Old 08-04-2008, 05:52 AM Re: how to rotate and crop a transparent png?plz solve my problem
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
PHP Code:
exec("convert {$source_png} -rotate 90 -gravity center -crop 200x200+0+0 {$target_png}"); 
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 08-04-2008, 05:54 AM Re: how to rotate and crop a transparent png?plz solve my problem
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Never manipulate images with php because there is specialized software made for exactly that purpose. ImageMagick.org.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 08-08-2008, 05:20 AM Re: how to rotate and crop a transparent png?plz solve my problem
Junior Talker

Posts: 4
Trades: 0
Hi mtishetsky,

I have worked on your solution for rotating and cropping images and it is working perfectly, so, thankyou very much for your quick response to my problem.


chinna_ann is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how to rotate and crop a transparent png?plz solve my problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34800 seconds with 12 queries