[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Php png and merged arrow png

Hello Experts,

I am trying to create a gradiented png and then merge with an arrow png...

But i get the two together but the arrow png has a white background...

Ive attached the code and the image...

Could you have a look please

Many Thanks Arrow
header("Content-type: image/png");
$width = 300;
$height = 40;

$start = '222b5e'; 
$end = '10173e'; 
 
$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = @imagecreate($width, $height);
 
for($y=0;$y<$height;$y++) {
  for($x=0;$x<$width;$x++) {
    if ($start_r == $end_r) {
      $new_r = $start_r;
    }
    $difference = $start_r - $end_r;
    $new_r = $start_r - intval(($difference / $height) * $y); 
    if ($start_g == $end_g) {
      $new_g = $start_g;
    }
    $difference = $start_g - $end_g;
    $new_g = $start_g - intval(($difference / $height) * $y);         
    if ($start_b == $end_b) {
      $new_b = $start_b;
    }
    $difference = $start_b - $end_b;
    $new_b = $start_b - intval(($difference / $height) * $y);
    $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    imagesetpixel($image, $x, $y, $row_color);
  }    
}


$image_1 = $image;
$image_2 = imagecreatefrompng('themes/default/img/menu-arrow.png');

imagealphablending($image_1, true);
imagesavealpha( $image_1, true );

imagealphablending($image_2, true);
imagesavealpha( $image_2, true );
 
imagecopy($image_1, $image_2, 0, 0, 0, 0, 40, 40);
imagepng($image_1);

Open in new window

0
maccaj51
Asked:
maccaj51
  • 10
  • 6
1 Solution
 
Ray PaseurCommented:
Png transparency can be complicated in PHP, but maybe this demonstration script will help.  See it in action on my server here:
http://www.laprbass.com/RAY_image_watermark.php

Feel free to look at the original image and the watermark image on my server, too.  It might be useful for you to post the arrow png here.

best regards, ~Ray
<?php // RAY_image_watermark.php
error_reporting(E_ALL);

// LOCATION OF THE IMAGES (COULD COME IN FROM $_GET URL STRING)
$original = 'http://www.LAPRBass.com/RAY_add_watermark_source_image.png';
$h2o_mark = 'http://www.LAPRBass.com/RAY_add_watermark_watermark_image.png';

// READ THE IMAGE AND THE WATERMARK FILE
// MAN PAGE http://php.net/manual/en/function.imagecreatefrompng.php
$im = ImageCreateFromPNG($original);
$wm = ImageCreateFromPNG($h2o_mark);

// ADD WATERMARK USING LOCAL FUNCTION
imagelogo($im, $wm, imagesx($im), imagesy($im), imagesx($wm), imagesy($wm));

// SHOW THE IMAGE
header('Content-type: image/png');
ImagePNG($im);

// A FUNCTION TO ADD THE LOGO WATERMARK
// SEE http://php.net/manual/en/function.imagealphablending.php#77085
function imagelogo (&$dst_image, $src_image, $dst_w, $dst_h, $src_w, $src_h)
{
    ImageAlphaBlending($dst_image,TRUE);
    ImageAlphaBlending($src_image,TRUE);
    ImageCopy($dst_image, $src_image, ($dst_w-$src_w), ($dst_h-$src_h), 0, 0, $src_w, $src_h);
}

Open in new window

0
 
maccaj51Author Commented:
Helped alot thank you!
0
 
Ray PaseurCommented:
Please read the grading guidelines for EE:
http://www.experts-exchange.com/help.jsp#hs=29&hi=403

Please tell me what was wrong so with the answer that caused you to mark the response down to a "B?"  Why didn't you post back with a question or give me a chance to correct any perceived deficiencies?  RSVP, thanks. ~Ray
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
maccaj51Author Commented:
Hi Ray,

Sorry about that. It wasnt exactly what I was looking for... But i managed to work round the issue using ur input as inspiration... I was have a look at the guidelines and re-assess the situation...
0
 
Ray PaseurCommented:
Thanks.  At least tell me what the problem was.  I know my example works correctly, but I couldn't figure out exactly what was wrong with your code.
0
 
maccaj51Author Commented:
What i am trying to do it merge a created gradient with attached code, to a arrow.png which i attached in the question....

But your code didnt seem to work in that way...



$start = "10173e";
$end = "222b5e";
$width = 40;
$height = 40;
 
 
$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = @imagecreate($width, $height);
 
for($y=0;$y<$height;$y++) {
  for($x=0;$x<$width;$x++) {
    if ($start_r == $end_r) {
      $new_r = $start_r;
    }
    $difference = $start_r - $end_r;
    $new_r = $start_r - intval(($difference / $height) * $y); 
    if ($start_g == $end_g) {
      $new_g = $start_g;
    }
    $difference = $start_g - $end_g;
    $new_g = $start_g - intval(($difference / $height) * $y);         
    if ($start_b == $end_b) {
      $new_b = $start_b;
    }
    $difference = $start_b - $end_b;
    $new_b = $start_b - intval(($difference / $height) * $y);
    $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    imagesetpixel($image, $x, $y, $row_color);
  }    
}

Open in new window

0
 
maccaj51Author Commented:
fyi rest of the code looks like this... still havent managed to make it work!!!!! thought i had!!!
$image = imagecreatefrompng($image);
$image_2 = imagecreatefrompng('./img/menu-arrow.png');

imagealphablending($image_2, true);
imagesavealpha($image_2, true);
imagecopy($image, $image_2, 0, 0, 0, 0, 100, 100);


header('Content-type: image/png');
imagepng($image);

Open in new window

0
 
Ray PaseurCommented:
What part is not working for you?  Are you satisfied with the gradient image you're creating?
0
 
maccaj51Author Commented:
yeah... but when i merge the two it turns black...
0
 
maccaj51Author Commented:
Thank you
0
 
maccaj51Author Commented:
Ive managed to get this code almost there... but it merges the menu-arrow with a transparent background and jsut place it above the gradient... Any ideas?
<?php



$width = 340;
$height = 40;
$start = "10173e";
$end = "222b5e";
 
 
$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = @imagecreate($width, $height);
 
for($y=0;$y<$height;$y++) {
  for($x=0;$x<$width;$x++) {
    if ($start_r == $end_r) {
      $new_r = $start_r;
    }
    $difference = $start_r - $end_r;
    $new_r = $start_r - intval(($difference / $height) * $y); 
    if ($start_g == $end_g) {
      $new_g = $start_g;
    }
    $difference = $start_g - $end_g;
    $new_g = $start_g - intval(($difference / $height) * $y);         
    if ($start_b == $end_b) {
      $new_b = $start_b;
    }
    $difference = $start_b - $end_b;
    $new_b = $start_b - intval(($difference / $height) * $y);
    $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    imagesetpixel($image, $x, $y, $row_color);
  }    
}
 

$image_2 = imagecreatefrompng('./img/menu-arrow.png');
imagefilter($image_2, IMG_FILTER_COLORIZE, $rgb['0'], $rgb['1'], $rgb['2']);
imagealphablending($image, true);
imagesavealpha($image, true);
imagealphablending($image_2, true);
imagesavealpha($image_2, true);
imagecopy($image, $image_2, 0, 0, 0, 0, 40, 100);


header('Content-type: image/png');
imagepng($image);


 
?>

Open in new window

0
 
maccaj51Author Commented:
The gradient script does not seem to output a proper png...

When i use an external photoshop created gradient the script works...

Very strange
0
 
maccaj51Author Commented:
Ive attached my files... Just cant get it to work. If anyone is still out there please see if you can help phpimg.zip phpimg.zip
0
 
maccaj51Author Commented:
I've requested that this question be deleted for the following reason:

Still unresolved!
0
 
Ray PaseurCommented:
Plonk!  What do you expect here?  We cannot write your code for you.  About the best we can do is provide answers to questions and teaching examples in the form of demonstration scripts.  We need to have some dialog if we are going to help you.  It would be helpful to have a link to the arrow png - the uploaded version is not giving me a useful image.  I don't know why, but it appears strange in Photoshop.

Going forward, please use the code snippet to post your code.  I am reluctant to open zip files.

Also, please take a moment to read the documentation on www.php.net.  All of the functions are documented with usage examples and user-contributed notes.  For example, see the recommendation from PHP about this function:
http://php.net/manual/en/function.imagecreate.php

I don't know what part of this is unresolved but you closed it two months ago with a marked down grade, after I had posted an example of code that did exactly what you asked about.  Where do we go from here?
0
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_maccaj51.php

Please see line 17.

<?php // RAY_temp_maccaj51.php
error_reporting(E_ALL);

header("Content-type: image/png");
$width = 300;
$height = 40;

$start = '222b5e'; 
$end = '10173e'; 
 
$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = imagecreateTrueColor($width, $height);
 
for($y=0;$y<$height;$y++) {
  for($x=0;$x<$width;$x++) {
    if ($start_r == $end_r) {
      $new_r = $start_r;
    }
    $difference = $start_r - $end_r;
    $new_r = $start_r - intval(($difference / $height) * $y); 
    if ($start_g == $end_g) {
      $new_g = $start_g;
    }
    $difference = $start_g - $end_g;
    $new_g = $start_g - intval(($difference / $height) * $y);         
    if ($start_b == $end_b) {
      $new_b = $start_b;
    }
    $difference = $start_b - $end_b;
    $new_b = $start_b - intval(($difference / $height) * $y);
    $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    imagesetpixel($image, $x, $y, $row_color);
  }    
}


$image_1 = $image;
$image_2 = imagecreatefrompng('http://filedb.experts-exchange.com/incoming/2011/05_w19/t456862/menu-arrow.png');

imagealphablending($image_1, true);
imagesavealpha( $image_1, true );

imagealphablending($image_2, true);
imagesavealpha( $image_2, true );
 
imagecopy($image_1, $image_2, 0, 0, 0, 0, 40, 40);
imagepng($image_1);
                                  
// http://filedb.experts-exchange.com/incoming/2011/05_w19/t456862/menu-arrow.png

Open in new window

HTH, ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now