Is there efficient way to replace alpha channel of target image with contents from another image (eg. red channel)? Now I'm using this code, but it's the slowest part of the whole app. Is it possible to use imagelayereffect() or maybe imagefilter() somehow to get the same result? Or maybe it is possible to break this into separate actions, eg. processing mask pixels with alpha = 0, alpha = 127 (it's in the code below), and any other values?
function imagealphamask( &$picture, $mask ) {
// Get sizes and set up new picture
$xSize = imagesx( $picture );
$ySize = imagesy( $picture );
$newPicture = imagecreatetruecolor( $xSize, $ySize );
imagesavealpha( $newPicture, true );
imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) );
// Resize mask if necessary
if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) {
$tempPic = imagecreatetruecolor( $xSize, $ySize );
imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) );
imagedestroy( $mask );
$mask = $tempPic;
}
// Perform pixel-based alpha map application
for( $x = 0; $x < $xSize; $x++ ) {
for( $y = 0; $y < $ySize; $y++ ) {
$alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );
$alpha = 127 - floor( $alpha[ 'red' ] / 2 );
if ($alpha == 127) {
continue;
}
$color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );
}
}
imagedestroy( $picture );
$picture = $newPicture;
}
Related
I speak a little English.
This is very good, it works:
<?php
header( 'Content-Type: image/png' );
$imgname = '../assets/images/lock-224x224.png'; // https://i.imgur.com/mYT7xoo.png
$im = imagecreatefrompng( $imgname );
imagealphablending( $im, true );
imagesavealpha( $im, true );
imagefilter ( $im, IMG_FILTER_COLORIZE, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) );
$im = imagerotate( $im, rand( 0, 360 ), 0 );
$im = imagescale( $im, -1, 224 );
imagepng( $im );
imagedestroy( $im );
?>
Source (transparent) [lock-224x224.png] image:
I would like transparency or colorized background, not black.
Now: black. Please see the created image:
Solved:
$im = imagecreatefrompng( $imgname );
$im = imagerotate( $im, rand( 0, 360 ), imagecolorallocatealpha( $im, 0, 0, 0, 127 ) );
imagealphablending( $im, false );
$im = imagescale( $im, -1, 224 );
imagefilter ( $im, IMG_FILTER_COLORIZE, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) );
imagesavealpha( $im, true );
imagepng( $im );
I want to increase the width of image after created it using imagefilledrectangle.
Here is my code.
$image = imagecreate($img_width+10, $img_height + $text_height);
$black = imagecolorallocate ($image, 0, 0, 0);
$white = imagecolorallocate ($image, 255, 255, 255);
imagefill( $image, 0, 0, $white );
if ( $print ) {
imagestring($image, 10, 50, $img_height+2, $text, $black );
}
$location = 10;
for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
$cur_size = $location + ( substr($code_string, ($position-1), 1) );
if ( strtolower($orientation) == "horizontal" )
imagefilledrectangle( $image, $location, 0, $cur_size, $img_height, ($position % 2 == 0 ? $white : $black) );
else
imagefilledrectangle( $image, 0, $location, $img_width, $cur_size, ($position % 2 == 0 ? $white : $black) );
$location = $cur_size;
}
return $image;
I'm creating a line of hexagon-masked images using GD but I'm unable to work out how to create transparency around each mask. The collage itself works perfectly but it adds a white, not transparent background to the image, despite setting 127 on the alpha argument in the imagecolorallocatealpha function. The code is based on this SO answer.
$count = 0;
// Full size image
$dest = imagecreatetruecolor( 195 * count( $images ), 230 );
// Loop image array
foreach ( $images as $image_array ) {
// Get image source and create raw format
$image_src = wp_get_attachment_image_src( $image_array['image'], 'hexagon' );
$raw = strpos( 'png', $image_src[0] !== false ) ? imagecreatefrompng( $image_src[0] ) : imagecreatefromjpeg( $image_src[0] );
$w = imagesx( $raw );
$h = imagesy( $raw );
/* Shape(ish)
/\
| |
\/
*/
$points = array(
0.5 * $w, 0,
0, 0.23 * $h,
0, 0.72 * $h,
0.5 * $w, $h,
$w, 0.72 * $h,
$w, 0.23 * $h
);
// Create the mask
$mask = imagecreatetruecolor( $w, $h );
imagefilledpolygon( $mask, $points, 6, imagecolorallocate( $mask, 255, 0, 0 ) );
// New image
$image = imagecreatetruecolor( $w, $h );
imagealphablending( $image, false );
imagesavealpha( $image, true );
// Transparency
$transparent = imagecolorallocatealpha( $raw, 255, 255, 255, 127 );
imagefill( $image, 0, 0, $transparent );
// Pixel mapping
for( $x = 0; $x < $w; $x++ ) {
for ( $y=0; $y < $h; $y++ ) {
$m = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );
if( $m['red'] ) {
$color = imagecolorsforindex( $raw, imagecolorat( $raw, $x, $y ) );
imagesetpixel( $image, $x, $y, imagecolorallocatealpha( $image,
$color['red'],
$color['green'],
$color['blue'],
$color['alpha']
)
);
}
}
}
// Merge to the original image
imagecopymerge( $dest, $image, ( 195 * $count ), 0, 0, 0, imagesx( $image ), imagesy( $image ), 100 );
$count++;
}
$path = get_template_directory() . '/assets/images/tile_image_one.png';
imagepng( $dest, $path, 0, NULL );
imagedestroy( $dest );
You need
imagesavealpha( $dest, true );
And if you plan to overlap the transparent bits of hexagon,
imagealphablending( $dest, true );
Hopefully someone will be able to tell me how to keep user quotes when generating an image. What I mean is if a user submits words like:
don't
or
John said "hello"
how can I keep them in the generated image. Currently it turns to
don\t
Here is my code:
<?php
function formattextimg( $text, $width = 960, $color = array( 0, 0, 0 ),$bgcolor = array( 255, 255, 255 ), $font = 2 ) {
$width = ( ( isset( $width ) && is_numeric( $width ) ) ? ( ( $width >= 100 ) ? (int)$width : 100 ) : 960 );
$text = str_replace( array( '<b>', '</b>', '<strong>', '</strong>' ), '|', $text );
$text_b = preg_split( '/\|/', $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
foreach($text_b as $k => $tb){if($k%2!=0){$textbold[($tb[1]-1)]=$tb[0];}}
$text = str_replace('|','',$text);
for( $i = 0; $i < strlen( $text ); $i++ ) {
if( $string_c >= ( $width - ( (int)(imagefontwidth( $font ) / 2 ) ) ) ) {
$space = strrpos( $string, ' ' );
$string_sub = substr( $string, 0, $space );
$i = $i - ( strlen( $string ) - $space ) + 1;
$strings[] = $string_sub;
$string = '';
$string_c = 0;
}
$string .= $text{$i};
$string_c += imagefontwidth( $font );
}
$strings[] = $string;
$im = imagecreatetruecolor( $width, ( imagefontheight( $font ) * ( count( $strings ) ) ) );
imagesavealpha( $im, true );
$trans = imagecolorallocate( $im, $bgcolor[0], $bgcolor[1], $bgcolor[2] );
imagefill( $im, 0, 0, $trans );
$color = imagecolorallocate( $im, $color[0], $color[1], $color[2] );
$black = imagecolorallocate( $im, 0, 0, 0 );
foreach( $strings as $pos => $string ) {
imagestring( $im, $font, 5, ( $pos * imagefontheight( $font ) ), $string, $color );
}
$len = 0;
foreach( $strings as $pos => $string ) {
$len += ( strlen( $string ) + 1 );
if( count( $textbold ) > 0 ) {
foreach( $textbold as $cpos => $word ) {
if( $cpos <= $len ) {
$wpos = strpos( $string, $word );
if( $wpos !== false ) {
imagestring( $im, $font,
( $wpos * imagefontwidth( $font ) )+1,
( ( $pos ) * imagefontheight( $font ) ),
$word, $color
);
unset( $textbold[$cpos] );
}
}
}
}
}
return $im;
}
header( 'Content-type: image/png' );
imagepng( formattextimg( $text,300,array( 255, 255, 255 ),array( 128, 128, 128 ),16 ) );
exit;
?>
If that is not possible can someone please tell me how to remove the:
'
and
"
I tried:
$text=$_REQUEST['text'];
$text=str_replace('"', "", $text);
$text=str_replace("'", "", $text);
But it did not work.
thanks
This is caused by magic_quotes_gpc being enabled on the server.
You can reverse the effect of magic_quotes_gpc by calling stripslashes if get_magic_quotes_gpc (manual) returns true:
$text = $_REQUEST['text'];
if (get_magic_quotes_gpc())
{
$text = stripslashes($text);
}
It's important that you actually check the value of get_magic_quotes before calling stripslashes, as this feature has been deprecated in PHP 5.3 and removed altogether from PHP 5.4.
How I can make a gradient between 2 or more ellipses with GD?
The following is an ellipse:
<?php
header( "Content-type: image/gif" );
$imagen = imagecreatefrompng('tras.png');
$morado = array( 'r' => 186, 'g' => 0, 'b' => 255 );
$amarillo = array( 'r' => 255, 'g' => 220, 'b' => 0 );
$pasos = 2000;
$incr_r = ( $amarillo['r'] - $morado['r'] ) / $pasos;
$incr_g = ( $amarillo['g'] - $morado['g'] ) / $pasos;
$incr_b = ( $amarillo['b'] - $morado['b'] ) / $pasos;
$cx = imagesx( $imagen ) / 3;
$cy = imagesy( $imagen ) / 3;
$ancho = 240;
$alto = 140;
$incr_x = $ancho / $pasos;
$incr_y = $alto / $pasos;
$r = $morado['r'];
$g = $morado['g'];
$b = $morado['b'];
for( $i = 0; $i < $pasos; $i++ ){
$color = imagecolorallocate( $imagen, $r, $g, $b );
imagefilledellipse( $imagen, $cx, $cy, $ancho, $alto, $color );
imagefilledellipse( $imagen, $ñ, $k, $ancho, $alto, $color );
$r += $incr_r;
$g += $incr_g;
$b += $incr_b;
$ancho -= $incr_x;
$alto -= $incr_y;
}
imagegif( $imagen );
imagedestroy( $imagen );
?>
Model...
This...
http://img88.imageshack.us/img88/11/capturazv.png
without cutting
It is possible that the class here will help you, although I haven't tested it.