Link to home
Start Free TrialLog in
Avatar of maccaj51
maccaj51Flag for Afghanistan

asked on

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 User generated image
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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

Avatar of maccaj51

ASKER

Helped alot thank you!
Please read the grading guidelines for EE:
https://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
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...
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.
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

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

What part is not working for you?  Are you satisfied with the gradient image you're creating?
yeah... but when i merge the two it turns black...
Thank you
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

The gradient script does not seem to output a proper png...

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

Very strange
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
I've requested that this question be deleted for the following reason:

Still unresolved!
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?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial