PHP Classes

File: PHP_toolbucket/SoftMoon/imagecopymerge_alpha.php

Recommend this page to a friend!
  Classes of Joseph  >  Rainbow Maker  >  PHP_toolbucket/SoftMoon/imagecopymerge_alpha.php  >  Download  
File: PHP_toolbucket/SoftMoon/imagecopymerge_alpha.php
Role: Auxiliary script
Content type: text/plain
Description: PHP utility function
Class: Rainbow Maker
Create transparent gradient images
Author: By
Last change:
Date: 10 years ago
Size: 2,439 bytes
 

Contents

Class file image Download
<?php

// this will only handle alpha-transparency in the source image
// transparency in the destination image where it is overlayed by the source will be lost...
Function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
// lifted from Sina Salek’s comments on http://us.php.net/manual/en/function.imagecopymerge.php
// modified adaption by SoftMoon WebWare
/**
    * PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
    * by Sina Salek
    *
    * Bugfix by Ralph Voigt (bug which causes it
    * to work only for $src_x = $src_y = 0.
    * Also, inverting opacity is not necessary.)
    * 08-JAN-2011
    *
    **/
   
if (100==$pct)
       
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
    else {

       
// creating a cut resource
       
$cut = imagecreatetruecolor($src_w, $src_h);

       
// copying relevant section from background to the cut resource
       
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h, 100);

       
// copying relevant section from watermark to the cut resource
       
imagecopy($dupl, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

       
// insert cut resource to destination image
       
imagecopymerge($dst_im, $dupl, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);

       
imagedestroy($dupl); } }

// this will handle alpha-transparency in both the source and destination images
Function imagecopymerge_alpha2($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
   
$pct=round(127-($pct/100)*127); // 0 <= $pct <= 100 convert opacity percentage to alpha-channel value
   
for ($x=0; $x<$src_w; $x++) { for ($y=0; $y<$src_h; $y++) {
       
$src_pixel=imagecolorsforindex($src_im, imagecolorat($src_im, $x, $y));
       
$dst_pixel=imagecolorsforindex($dst_im, imagecolorat($dst_im, $dst_x+$x, $dst_y+$y));
       
$pixel=imagecolorallocatealpha($dst_im,
           
$src_pixel['red'],
           
$src_pixel['green'],
           
$src_pixel['blue'],
           
$tf=min($src_pixel['alpha'] + $pct, 127));
       
imagealphablending($dst_im, TRUE);
       
imagesetpixel($dst_im, $dst_x+$x, $dst_y+$y, $pixel);
       
imagecolordeallocate($dst_im, $pixel);
       
$int_pixel=imagecolorsforindex($dst_im, imagecolorat($dst_im, $dst_x+$x, $dst_y+$y));
       
$pixel=imagecolorallocatealpha($dst_im,
           
$int_pixel['red'],
           
$int_pixel['green'],
           
$int_pixel['blue'],
           
$dst_pixel['alpha']*$tf/127 );
       
imagealphablending($dst_im, FALSE);
       
imagesetpixel($dst_im, $dst_x+$x, $dst_y+$y, $pixel);
       
imagecolordeallocate($dst_im, $pixel); } } }

?>