'Can I modify a GD's modified image with GD too?
I am trying to put a modified image with GD PHP into another GD script too.
When, the first part of the script (blur, resize, fit if smaller than 1920x1080) and, the second part (add an alpha friendly filigrane) are separated. Both scripts works fine. But when I try to made an unique script with that. Filigrane part doesn't works !
I don't want save my first file before apply second part. I want to edit em in one time.
Can you think it's possible ? I have read the PHP documentation and try somes possibilities of GD but 2h after it still not working ..
Thats the commentated code with the first and the second part ...
<?php
////////////////////////////////////////
/////////////////////
////// set default images into variables
//////
header ("Content-type: image/png");
//////
$image = "t.PNG"; // test background image
$filigraneimage = "logo.png"; // test logo filigrane
////////////////////////////////////////
/////////////////////
////// token code generator
//////
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$aleatorycode = generateRandomString(16);
$UNIQ = htmlspecialchars($aleatorycode);
$str1 = substr($UNIQ, 0, 3);
$str2 = substr($UNIQ, 13, 16);
$TOKEN = $str1 . $str2;
////////////////////////////////////////
/////////////////////
////// Call the script
//////
resize_blur_fitoriginal($image, $filigraneimage, 1920, 1080, $TOKEN); // I need final images 1920x/1080 (16:9)
////////////////////////////////////////
/////////////////////
////// Define the script into a function
//////
// F U N C T I O N B L U R A N D R E S I Z E
function resize_blur_fitoriginal($image, $filigraneimage, $width, $height, $token){ // Get the image source and the final desire dimensions
$info = getimagesize($image);
//$mimetype = $info['mime']; // other way to get mimetype
$mimetype = image_type_to_mime_type($info[2]);
$allowTypes = array('image/jpeg','image/png','image/gif');
if(in_array($mimetype, $allowTypes)){
switch ($mimetype) {
case 'image/jpeg':
$image = imagecreatefromjpeg($image);
break;
case 'image/gif':
$image = imagecreatefromgif($image);
break;
case 'image/png':
$image = imagecreatefrompng($image);
break;
default:
die('Invalid image type.');
}
} else {
echo 'Is not an image';
}
$image2 = $image; // Save a copy to be used for front image
$wor = imagesx($image); // Get original image width
$hor = imagesy($image); // Get original image height
if ($hor >= $wor){ // If is vertical*******************************************************************************************************
if ($wor >= $width){ // If image source is bigger than final desire dimensions
$hcenter = ($hor/2)-($height/2); // center image source in height
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $wor, $hor, $wor, $hor);
} else { // If image source is not bigger than final desire dimensions
$hcenter = ($hor/2)-($height/2); // center image source in height
$hnu = ($hor*$width)/$wor;
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
}
} else { // If is portrait rectangular****************************************************************************************************
$ratio = $wor/$hor;
if($ratio > 1.777777777777778){ // If is portrait larger than 16:9
$wnu = ($wor*$height)/$hor;
$wcenter = ($wnu/2)-($width/2); // center image in width
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, -$wcenter, 0, 0, 0, $wnu, $height, $wor, $hor);
} else { // If portrait is not larger than 4:3
$hnu = ($wor*$height)/$hor;
$hcenter = ($hnu/2)-($height/2); // center image source in height
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
}
}
// Blur Image
for ($x=1; $x <=65; $x++){
imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($back, IMG_FILTER_SMOOTH,99);
imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
// Getting the dimensions of the image
$src_w = imagesx($image2);
$src_h = imagesy($image2);
// Do some math to figure out which way we'll need to crop the image
// to get it proportional to the new size, then crop or adjust as needed
$x_ratio = $width / $src_w;
$y_ratio = $height / $src_h;
if (($src_w <= $width) && ($src_h <= $height)) {
$new_w = $src_w;
$new_h = $src_h;
} elseif (($x_ratio * $src_h) < $height) {
$new_h = ceil($x_ratio * $src_h);
$new_w = $width;
} else {
$new_w = ceil($y_ratio * $src_w);
$new_h = $height;
}
$front = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($front, $image2, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
if ($new_h >= $new_w){ // If is vertical image
$wctr = ($new_w/2)-($width/2);
imagecopymerge($back, $front,-$wctr, 0, 0, 0, $new_w, $new_h, 100);
} else { // if is portrait
$hctr = ($new_h/2)-($height/2);
imagecopymerge($back, $front,0, -$hctr, 0, 0, $new_w, $new_h, 100);
}
// output new file
$tokename = $token . ".png";
$tester = file_get_contents(imagepng($back));
// imagepng($back, $token, 100); // doesnt work
/* imagedestroy($back);
imagedestroy($front); */
////////////////////////////////////////
/////////////////////
////// S E C O N D P A R T O F T H E S C R I P T, A P P L Y T H E F I L I G R A N E L O G O
//////
// Load pictures
$filigrane_src = $filigraneimage; // the filigrane. Is the source image
$filigrane_dst = $tester; // The original image to apply the filigrane. Is the destination image of the first part
//$filigrane_dst = "00.png";
//Create each pictures
$filigrane_src = imagecreatefrompng($filigrane_src);
$filigrane_dst = imagecreatefrompng($filigrane_dst);
// Detect and save the width & height in respective vars
// source
$filigrane_width_src = imagesx($filigrane_src);
$filigrane_height_src = imagesy($filigrane_src);
// destination
$filigrane_width_dst = 1920;
$filigrane_height_dst = 1080;
// Apply alpha parameters
imagealphablending($filigrane_src, false); // true or false = same result :(
imagesavealpha($filigrane_src, true);
// Set supplementary transparent background to src // doesn't work
/*
$filigrane_transparent = imagecolorallocatealpha( $filigrane_src, 0, 0, 0, 0 );
imagefilledrectangle($filigrane_src, 0, 0, $filigrane_width_src, $filigrane_height_src, $filigrane_transparent); */
// Put the filigrane, $filigrane_src, at the bottom, right, corner ! Also calculate coordonates and ut filigrane logo (src) into image dst
$filigrane_dst_x = $filigrane_width_dst - $filigrane_width_src;
$filigrane_dst_y = $filigrane_height_dst - $filigrane_height_src;
$filigrane_dst_im = $filigrane_dst;
$filigrane_src_im = $filigrane_src;
$filigrane_dst_x = $filigrane_dst_x;
$filigrane_dst_y = $filigrane_dst_y;
$filigrane_src_x = 0;
$filigrane_src_y = 0;
$filigrane_src_w = $filigrane_width_src;
$filigrane_src_h = $filigrane_height_src;
$filigrane_pct = 40; // 50 = transparency of non-alpha visibles elements
//////////////////
//////////
// function imagecopymerge_alpha implemented into RESIZE_BLUR_FILL function
// creating a cut resource
$filigrane_cut = imagecreatetruecolor($filigrane_src_w, $filigrane_src_h);
// copying relevant section from background to the cut resource
imagecopy($filigrane_cut, $filigrane_dst_im, 0, 0, $filigrane_dst_x, $filigrane_dst_y, $filigrane_src_w, $filigrane_src_h);
// copying relevant section from watermark to the cut resource
imagecopy($filigrane_cut, $filigrane_src_im, 0, 0, $filigrane_src_x, $filigrane_src_y, $filigrane_src_w, $filigrane_src_h);
// insert cut resource to destination image
imagecopymerge($filigrane_dst_im, $filigrane_cut, $filigrane_dst_x, $filigrane_dst_y, 0, 0, $filigrane_src_w, $filigrane_src_h, $filigrane_pct);
//
///////
///////////////
// Show the result
imagepng($filigrane_dst);
}
?>
Thanks for your time !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
