Retrive PHP function confusion [duplicate] - php

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 months ago.
The project i'm working right now require to use some color randomization. I have atleast 20 asset (text,shape,line,polygon ...etc) that require color randomization ...so i decided to store the randomization RGB in a function so i could keep reusing it and make the PHP file smaller but it didn't work out as expected
<?php
header ('Content-Type: image/jpeg'); // make PHP when access .jpg
$im = imagecreatetruecolor(100, 100); // allocate image with 100 width and 100 height
function randomrgb() {
imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); // generate 0 between 255
}
$cbc = randomrgb(); // store randomization on variable first i do not know how to call functions directly
imagefill($im, 0, 0, $cbc); // fill the background with $cbc which is equal to random RGB function
imagejpeg($im);
imagedestroy($im);
?>
I expected the background color to become random but it always black also i do not know how to call the function directly here imagefill($im, 0, 0, $cbc); without storing it first on variable something like imagefill( $im, 0, 0, randomrgb(); ); i know it possible but i just do not know how .. sorry in advance if this is confusing

In PHP (and many other programming languages) , any variable used inside a function is by default limited to the local function scope .
For your case, please
pass the $im (after you used imagecreatetruecolor to create the image identifier) as parameter to the function; and
add return in your function, so as to assign the result to $cbc
Hence, change to:
<?php
header ('Content-Type: image/jpeg'); // make PHP when access .jpg
$im = imagecreatetruecolor(100, 100); // allocate image with 100 width and 100 height
function randomrgb($im) {
return imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); // generate 0 between 255
}
$cbc = randomrgb($im); // store randomization on variable first i do not know how to call functions directly
imagefill($im, 0, 0, $cbc); // fill the background with $cbc which is equal to random RGB function
imagejpeg($im);
imagedestroy($im);
?>

Related

Converting JPEG/PNGs to PNG for database storage but can't keep background transparency/colour when displaying, in PHP

I've a script I use to convert JPEGs or PNGs to PNGs for database storage, I encode the file into base64 and store it in a database, I pull the file from the database or cache and when displaying it on a website I just pull the image data from the database or cache and do a base64 decode on it and display it using a standard IMG tag.
The issue I'm having is, no matter if the background is transparent or what colour I create as the image's background it always shows up as black when being displayed on the webpage.
I've tried numerous of the questions on the right side and none of the answers seems to work for me.
Encode Function
public function encode($image, $resize = false, $dirLevel = '')
{
$vTempFileName = TEMP_DIR . '_'.rand(1111111, 9999999) . '.png';
// Convert image to PNG
$image = imagecreatefromstring(file_get_contents($image));
imagealphablending($image, true);
imagepng($image, $vTempFileName);
$vImageDetails = array(base64_encode(file_get_contents($vTempFileName)), filesize($vTempFileName));
// Remove temporary file after processing
#unlink($vTempFileName);
return $vImageDetails;
}
Decode Function
echo base64_decode($image);
I've also tried using the below two functions
To save transparency you need to add imagesavealpha See http://php.net/manual/en/function.imagesavealpha.php
public function encode($image, $resize = false, $dirLevel = '')
{
$vTempFileName = TEMP_DIR . '_'.rand(1111111, 9999999) . '.png';
// Convert image to PNG
$image = imagecreatefromstring(file_get_contents($image));
imagealphablending($image, true);
imagesavealpha($image, true);
imagepng($image, $vTempFileName);
$vImageDetails = array(base64_encode(file_get_contents($vTempFileName)), filesize($vTempFileName));
// Remove temporary file after processing
#unlink($vTempFileName);
return $vImageDetails;
}
imagefill will perform a flood fill on the selected coordinates with the desired color defined as $transparent See: http://php.net/manual/en/function.imagefill.php
$fillColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $fillColor);
To flood fill the adjoining color at the top left of the image with the desired fillColor.
Otherwise to add transparency use imagecolortransparent see http://php.net/manual/en/function.imagecolortransparent.php
$replaceBlack = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $replaceBlack);
to replace the color black as transparent.
This may or may not help you but... you really don't want to store images into a database if you can avoid it. Is there a good reason you are doing this? File systems are good at storing binary files. Databases are good at storing data. Use the appropriate tool for the job – store it as a file.

Trouble changing image size in PHP?

I have the following PHP code...
$destination_image_x = "235";
$destination_image_y = "230";
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);
$source_image_x = imagesx($temp_profile_picture_converted);
$source_image_y = imagesy($temp_profile_picture_converted);
$temp_profile_picture_converted = imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($temp_profile_picture_converted, $user_profile_picture_filename,'75');
imagedestroy($temp_profile_picture_converted);
The function of this code is to scale an image passed to it, and save it at a specified directory. I'm able to save the image using "imagejpeg" normally if I ommit the resizing snippet. The variable "$temp_profile_picture_converted" is assigned to a jpg image I created from the user's uploaded image with "imagecreatefromjpeg." (Or imagecreatefrompng, or imagecreatefromgif, etc.)
You are using the same variable $temp_profile_picture_converted twice in the following line. The function imagecopyresampled() returns a boolean and is overwriting the image this variable holds. The return value from this function is only to check success. Change it to:
if (! imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y)){
// then give error message...
}
UPDATE
However, you have other errors. You need to change the first parameter of imagejpeg(). I also changed the size vars from strings to numbers - not sure if it mattered.
imagejpeg($destination_image, $user_profile_picture_filename,75);
I successfully ran the following code
$destination_image_x = 235;
$destination_image_y = 230;
$source_image_x = imagesx($temp_profile_picture_converted);
$source_image_y = imagesy($temp_profile_picture_converted);
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);
imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($destination_image, $user_profile_picture_filename,75);
imagedestroy($temp_profile_picture_converted);
imagedestroy($destination_image);
Note that I also added the last statement imagedestroy($destination_image);

How to create and to merge a 2D transparent barcode on top of a JPG image in PHP?

Could you please see the below code and let me know how to write the $png on top of the $jpg image? The code shows the bacode image in the browser, but it does not save it and merge it with $jpg image. thanks for taking time and help.
$jpg= imagejpeg($image, '/applications/AMPPS/www/files/final-image-2.jpg');
require_once(dirname(__FILE__).'/tcpdf/tcpdf_barcodes_2d.php');
// set the barcode content and type
$barcodeobj = new TCPDF2DBarcode('Hello-this-is-a-test', 'DATAMATRIX');
// output the barcode as PNG image
$barcodeobj->getBarcodePNG(6, 6, array(0,0,0));
// the code stops to work from here
$png= imagepng($barcodeobj, '/applications/AMPPS/www/certificates/barcode.jpg');
imagecopymerge($png, $jpg, 0, 0, 0, 0, 50, 50, 100);
imagedestroy($image);
imagedestroy($png);

PHP set the attribute for an image with matrix values

as the topic,I need to set the attribute for an image or create an image with the attribute what I get from a client as the matrix values.
I found a function named imageconvolution,but it doesn't work out.Maybe I used it incorrectly.
here is the code:
<?php
$image = imagecreatefromgif('http://www.php.net/images/php.gif');
$emboss = array(array(0, 0, 100), array(0, 0, 200), array(0, 0, 1));
imageconvolution($image, $emboss, 1, 0);
header('Content-Type: image/png');
imagepng($image, null, null);
?>
the matrix values are used to scale or rotate or move the image.Is these code right?
I hope to find out someone to teach me.
Thanks a lot.
Scaling, rotation and moving are affine transforms. You cannot use convolution for this matrixes.
I think the easiest way to use the php Imagick extension. It has an affineTransformImage function which you can use: http://php.net/manual/en/imagick.affinetransformimage.php

PHP image color analysis with transparency

I am currently working on an application that needs to analyse a number of images and figure out what color they're closest to.
Therefore I found a code snippet that does exactly that:
function analyzeImageColors($im, $xCount =3, $yCount =3)
{
//get dimensions for image
$imWidth =imagesx($im);
$imHeight =imagesy($im);
//find out the dimensions of the blocks we're going to make
$blockWidth =round($imWidth/$xCount);
$blockHeight =round($imHeight/$yCount);
//now get the image colors...
for($x =0; $x<$xCount; $x++) { //cycle through the x-axis
for ($y =0; $y<$yCount; $y++) { //cycle through the y-axis
//this is the start x and y points to make the block from
$blockStartX =($x*$blockWidth);
$blockStartY =($y*$blockHeight);
//create the image we'll use for the block
$block =imagecreatetruecolor(1, 1);
//We'll put the section of the image we want to get a color for into the block
imagecopyresampled($block, $im, 0, 0, $blockStartX, $blockStartY, 1, 1, $blockWidth, $blockHeight );
//the palette is where I'll get my color from for this block
imagetruecolortopalette($block, true, 1);
//I create a variable called eyeDropper to get the color information
$eyeDropper =imagecolorat($block, 0, 0);
$palette =imagecolorsforindex($block, $eyeDropper);
$colorArray[$x][$y]['r'] =$palette['red'];
$colorArray[$x][$y]['g'] =$palette['green'];
$colorArray[$x][$y]['b'] =$palette['blue'];
//get the rgb value too
$hex =sprintf("%02X%02X%02X", $colorArray[$x][$y]['r'], $colorArray[$x][$y]['g'], $colorArray[$x][$y]['b']);
$colorArray[$x][$y]['rgbHex'] =$hex;
//destroy the block
imagedestroy($block);
}
}
//destroy the source image
imagedestroy($im);
return $colorArray;
}
Problem is that whenever I provide an image with transparency, GDLib consinders the transparency to be black, thus producing a wrong (much darker) output than is really the case.
For example this icon where the white area around the arrow is actually transparent:
example http://img651.imageshack.us/img651/995/screenshot20100122at113.png
Can anyone tell me how to work around this?
You need imageColorTransparent(). http://www.php.net/imagecolortransparent
Transparency is a property of the image, not of a color. So use something like $transparent = imagecolortransparent($im) to see if there is any transparency on your image, then just ignore that color in your $colorArray or have some other way to identify the transparent color in the return from your function. That all depends on how you're using the returned data.
--M

Categories