Re: [PHP] GD - import a PNG image and make transparant

2010-05-13 Thread Alex Davies
Hi Ash,

Thanks for your suggestion. I think this is where my confusion is. I
understand how to use imagecolorallocatealpha() to for example create a 50%
transparant colour, and apply it to a new rectangle for example.

I dont understand how to "apply" it to a new source image, for example
$src = imagecreatefrompng('test.png');
// Something here (maybe  imagecolorallocatealpha()) to make this
50% transparent- either on its own, or make it 50% transparent as part of a
copy onto a new image

I had thought that imagecopymerge would help me with this, but it seems not.

I'm currently looking through Karl's example to see if I can work it out,
but if anyone can point out a super-simple way of achieving the pseudo-code
above, i'd be very grateful!

Cheers,

Alex

On Thu, May 13, 2010 at 1:34 AM, Ashley Sheridan
wrote:

>  On Thu, 2010-05-13 at 00:12 +0100, Alex Davies wrote:
>
> Hi,
>
> I am trying to import a PNG image from disk, place it on top of a
> transparant image created in GD and output it to the browser. In the case of
> a low opacity setting, I would expect to see the background colour from the
> HTML page.
>
> If I set the opacity to 0, everything works - I end up with a transparant
> image.
>
> However, if I set it for any value >0 (even 1) instead of  a
> very-faint-image the whole thing goes black. As the opacity level goes up
> from 0, the amount of black reduces and the amount of imported image
> increases - but this is not what I want.
>
> I am using this code:
>
>  $src = imagecreatefrompng('test.png');
> $img_width  = imagesx($src);
> $img_height = imagesy($src);
>
> // Create trans image
> $dest = imagecreatetruecolor($img_width, $img_height);
> //imagesavealpha($dest, true); // This has no effect it appears
> $trans_colour = imagecolorallocatealpha($dest, 0, 255, 0, 128);
>
> // Make the background transparent
> imagecolortransparent($dest, $trans_colour);
> //imagefill($dest, 0, 0, $trans_colour); // This does not work
>
> // Merge src on top of dest, with opacity of 1 in this case
> imagecopymerge($dest, $src, 0, 0, 0, 0, $img_width, $img_height, 1);
>
> // Output and free from memory
> header('Content-Type: image/png');
> imagepng($dest);
> ?>
>
> The images that this outputs, at opacity levels 0,1 and 80 on a red and
> green background (screenshots of a HTML page) can be downloaded 
> fromhttp://www.box.net/shared/h9zn4tjgro
>
> Any help appreciated!
>
> Cheers,
>
> Alex
>
>
> How exactly are you setting the opacity for the image? The traditional way
> is to use imagecolorallocatealpha() on the source.
>
>   Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>


-- 
Alex Davies

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the sender immediately by e-mail and delete this e-mail permanently.


[PHP] GD - import a PNG image and make transparant

2010-05-12 Thread Alex Davies
Hi,

I am trying to import a PNG image from disk, place it on top of a
transparant image created in GD and output it to the browser. In the case of
a low opacity setting, I would expect to see the background colour from the
HTML page.

If I set the opacity to 0, everything works - I end up with a transparant
image.

However, if I set it for any value >0 (even 1) instead of  a
very-faint-image the whole thing goes black. As the opacity level goes up
from 0, the amount of black reduces and the amount of imported image
increases - but this is not what I want.

I am using this code:



The images that this outputs, at opacity levels 0,1 and 80 on a red and
green background (screenshots of a HTML page) can be downloaded from
http://www.box.net/shared/h9zn4tjgro

Any help appreciated!

Cheers,

Alex


Re: [PHP] Transparent PNGs: Merging

2009-12-12 Thread Alex Davies
Hi Ash,

Thanks for your suggestion. Unfortunately,that did not help but i've found a
post via Google which gave a simple (and slightly counter-intuitive)
solution - replace

imagecopymerge($img2, $img, $yoffset, 5, 0, 0, imagesx($img), imagesy($img),
100);
with
imagecopy($img2, $img, $yoffset, 5, 0, 0, imagesx($img), imagesy($img));

And it works :)

It seems imagecopymerge does not work well with PNGs and Alpha Channels.

Many thanks for your help,

Alex

On Sat, Dec 12, 2009 at 11:20 AM, Ashley Sheridan
wrote:

>  On Sat, 2009-12-12 at 05:02 +0000, Alex Davies wrote:
>
> Hi All,
>
> I apologise if this is a newbie post; i'm new to transparent graphics
> (PNGs in my case).
>
> I have a transparent PNG on disk, which I want PHP to load into memory
> and add some text to ("watermarking", I guess). This seems to be what
> is achieved @ http://php.ca/manual/en/image.examples.merged-watermark.php
>
> I have taken the following code almost entirely from the manual, but
> can not understand what I have done wrong:
>
> 
> //Create the image
> $img = @imagecreatetruecolor($width, $height)
>   or die("Cannot Initialize new GD image stream");
>
> //Make it transparent
> imagesavealpha($img, true);
> $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
> imagefill($img, 0, 0, $trans_colour);
>
> //Get the text color
> $text_color = imagecolorallocate($img, 255, 0, 0);
>
> //Draw the string
> imagestring($img, $font_size, 0, 0,  $string, $text_color);
>
> // Load background image, find position, print
> $img2 = imagecreatefrompng('xxx.png');
> $yoffset = (imagesx($img2)/2) - (imagesx($img)/2);
> imagecopymerge($img2, $img, $yoffset, 5, 0, 0, imagesx($img),
> imagesy($img), 100);
>
> // Output the merged images
> header ("Content-type: image/png");
> imagepng($img2);
> 
>
> This code prints the background image with transparancy removed with a
> light grey, and the text image in red on a black background in the
> correct position.
> If I change it to imagepng($img), the text image is printed correctly
> (transparency and all)
>
> Suggestions appreciated, i'm lost!
>
> Many thanks,
>
> Alex
>
>
>
> The only thing I can obviously see is that you've told it to ignore
> transparency on $img2 by not asking it to preserve it (the default is to
> ignore). PNG doesn't handle transparency in the same way a GIF does, each
> pixel can have a varying degree of opacity, which allows whatever is beneath
> it to show through. Add the imagesavealpha($img, true); line for $img2 and
> see what that does for you.
>
>
>   Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>


-- 
Alex Davies

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the sender immediately by e-mail and delete this e-mail permanently.


[PHP] Transparent PNGs: Merging

2009-12-11 Thread Alex Davies
Hi All,

I apologise if this is a newbie post; i'm new to transparent graphics
(PNGs in my case).

I have a transparent PNG on disk, which I want PHP to load into memory
and add some text to ("watermarking", I guess). This seems to be what
is achieved @ http://php.ca/manual/en/image.examples.merged-watermark.php

I have taken the following code almost entirely from the manual, but
can not understand what I have done wrong:


//Create the image
$img = @imagecreatetruecolor($width, $height)
  or die("Cannot Initialize new GD image stream");

//Make it transparent
imagesavealpha($img, true);
$trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $trans_colour);

//Get the text color
$text_color = imagecolorallocate($img, 255, 0, 0);

//Draw the string
imagestring($img, $font_size, 0, 0,  $string, $text_color);

// Load background image, find position, print
$img2 = imagecreatefrompng('xxx.png');
$yoffset = (imagesx($img2)/2) - (imagesx($img)/2);
imagecopymerge($img2, $img, $yoffset, 5, 0, 0, imagesx($img),
imagesy($img), 100);

// Output the merged images
header ("Content-type: image/png");
imagepng($img2);


This code prints the background image with transparancy removed with a
light grey, and the text image in red on a black background in the
correct position.
If I change it to imagepng($img), the text image is printed correctly
(transparency and all)

Suggestions appreciated, i'm lost!

Many thanks,

Alex

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 100% CPU Usage somewhere in script - how to find it?

2009-01-18 Thread Alex Davies
Hi Eric, Richard,

Thank-you for your suggestions. I had already tried xdebug - the problem
that I had is that because of the CPU overload it was showing pretty much
everything as screwed! However by using absolute CPU time measures I managed
to work out what method was involved, and on removing it the situation is
completely resolved.

I still wonder if there is a neater way to do this!

Cheers,

Alex

On Sun, Jan 18, 2009 at 2:43 PM, Eric Butera  wrote:

> On Sun, Jan 18, 2009 at 3:16 AM, Alex Davies  wrote:
> > Hi,
> >
> > I use a (externally developed) library within my application which
> > used to work fine. However, after a recent update (which unfortunately
> > we can't roll back due to dependencies) I have crazy CPU loads - there
> > are ~20 cores in total on the webservers and they have gone from an
> > average utilization of ~5% to >99%. I am using lighttpd, and so I can
> > see the php-cgi processes using 99% all my CPU!
> >
> > I have an extremely basic understanding of how to track this down, and
> > using XDebug and KCacheGrind I think that I have a pretty good idea
> > which area the problem lies in, but the "problem" is that because
> > whatever it is that has broken is using up the whole CPU everything
> > else slows down. Furthermore, it would appear that a large part of
> > what gets scheduled is luck because sometimes things are instant and
> > the same activity a short while later can take forever. However, based
> > on absolute CPU time's from this XDebug/KCacheGrind analysis and a gut
> > hunch based upon what has changed recently I think I know roughly what
> > it is. I do have a diff of the recent code change, and I have looked
> > at it to no avail.
> >
> > My questions are:
> > - Is there a better way to see what is using up the CPU? There must be
> > a single loop or something talking to an external service, that is
> > using so much CPU user time. Is there a super-quick way to do to this?
> > I really need to know what function (or even line) is using all that
> > CPU time, because I can't spot it in the code. Perhaps this
> > information can be obtained from XDebug but I can't find it!
> > - If there is no such magic answer to this question, what is the
> > correct way to find such a phantom problem? Are there any other
> > applications that might be better?
> >
> > If anyone has any personal recommendations for Consultants able to
> > look at and fix this sort of problem that would be much appreciated
> > too; I am now well out of my depth and sinking fast!
> >
> > Many thanks for any help,
> >
> > Alex
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> XDebug can produce a result showing time spent in each function across
> an entire run.  So if you were somehow able to turn on xdebug and
> reproduce this issue, it would indeed say what function/method this
> was happening in.
>
> http://xdebug.org/docs/profiler
>
> Sounds like that'd be pretty tricky, so good luck.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Alex Davies

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the sender immediately by e-mail and delete this e-mail permanently.


[PHP] 100% CPU Usage somewhere in script - how to find it?

2009-01-18 Thread Alex Davies
Hi,

I use a (externally developed) library within my application which
used to work fine. However, after a recent update (which unfortunately
we can't roll back due to dependencies) I have crazy CPU loads - there
are ~20 cores in total on the webservers and they have gone from an
average utilization of ~5% to >99%. I am using lighttpd, and so I can
see the php-cgi processes using 99% all my CPU!

I have an extremely basic understanding of how to track this down, and
using XDebug and KCacheGrind I think that I have a pretty good idea
which area the problem lies in, but the "problem" is that because
whatever it is that has broken is using up the whole CPU everything
else slows down. Furthermore, it would appear that a large part of
what gets scheduled is luck because sometimes things are instant and
the same activity a short while later can take forever. However, based
on absolute CPU time's from this XDebug/KCacheGrind analysis and a gut
hunch based upon what has changed recently I think I know roughly what
it is. I do have a diff of the recent code change, and I have looked
at it to no avail.

My questions are:
- Is there a better way to see what is using up the CPU? There must be
a single loop or something talking to an external service, that is
using so much CPU user time. Is there a super-quick way to do to this?
I really need to know what function (or even line) is using all that
CPU time, because I can't spot it in the code. Perhaps this
information can be obtained from XDebug but I can't find it!
- If there is no such magic answer to this question, what is the
correct way to find such a phantom problem? Are there any other
applications that might be better?

If anyone has any personal recommendations for Consultants able to
look at and fix this sort of problem that would be much appreciated
too; I am now well out of my depth and sinking fast!

Many thanks for any help,

Alex

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php