Converting multiple hex color to image - php

is there a function that will convert hex colors to image and save it as png?
example :
$pixelrow1 ["000000","000000","000000"];
$pixelrow2 ["000000","FFFFFF","000000"];
$pixelrow3 ["FF0000","00FF00","0000FF"];
function convert_to_image($row1,$row2,$row3,$path_to_save) {
// Some function
}
convert_to_image($pixelrow1,$pixelrow2,$pixelrow3,"c:/image.png");
I really have no idea if is it possible or not, but i'm pretty sure that its possible because you can make image using php
The output should return like this :

You can do it like this, but hopefully your variables have more sensible names and you can use a loop:
<?php
$im = imagecreate(3,3);
$black = imagecolorallocate($im,0,0,0);
$white = imagecolorallocate($im,0xff,0xff,0xff);
$red = imagecolorallocate($im,0xff,0,0);
$green = imagecolorallocate($im,0,0xff,0);
$blue = imagecolorallocate($im,0,0,0xff);
# First row
imagesetpixel($im,0,0,$black);
imagesetpixel($im,1,0,$black);
imagesetpixel($im,2,0,$black);
# Second row
imagesetpixel($im,0,0,$black);
imagesetpixel($im,1,1,$white);
imagesetpixel($im,2,1,$black);
# Third row
imagesetpixel($im,0,2,$red);
imagesetpixel($im,1,2,$green);
imagesetpixel($im,2,2,$blue);
imagepng($im,"result.png");
?>

The real problem is not saving the data into the file you want.
The real problem is saving the data IN png format.
You should read how png saves the data.
Or you can play a little with PHP image resources.
Maybe this snippet of code can give you some advice:
<?php
header("Content-Type: image/png");
$im = #imagecreate(1, 1);
// Creates a 1x1 image resource
$background_color = imagecolorallocate($im, 0xFF, 0x00, 0x00);
// Adds a red background color to the only pixel in the image.
imagepng($im);
// Sends the image to the browser.
imagedestroy($im);
?>
If you wanna take a look at all the functions for the images:
http://php.net/manual/en/ref.image.php

Related

PHP function not displaying barcode using TrueType fonts

Hello I am using a function that I found in Internet to display a barCode using a TrueType font, here is the code:
//For displaying barcodes
//Arguments are:
// code Number you want outputted as a barcode
//You can use this script in two ways:
// From a webpage/PHP script <img src='/images/barcode.php?code=12345'/>
// Directly in your web browser http://www.example.com/images/barcode.php?code=12345
//Outputs the code as a barcode, surrounded by an asterisk (as per standard)
//Will only output numbers, text will appear as gaps
//Image width is dynamic, depending on how much data there is
header("Content-type: image/png");
$file = "barcode.png"; // path to base png image
$im = imagecreatefrompng($file); // open the blank image
$string = "123123123"; // get the code from URL
imagealphablending($im, true); // set alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
$black = imagecolorallocate($im, 0, 0, 0); // colour of barcode
$font_height=40; // barcode font size. anything smaller and it will appear jumbled and will not be able to be read by scanners
$newwidth=((strlen($string)*20)+41); // allocate width of barcode. each character is 20px across, plus add in the asterisk's
$thumb = imagecreatetruecolor($newwidth, 40); // generate a new image with correct dimensions
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, 40, 10, 10); // copy image to thumb
imagettftext($thumb, $font_height, 0, 1, 40, $black, 'B2FI25HRc.ttf', '*'.$string.'*'); // add text to image
//show the image
imagepng($thumb);
imagedestroy($thumb);
I cannot find the error why the function doesn't display the image. Any ideas? The font is in the same directory with the php function and I tried relative and absolute paths to the font with no results. Any suggestion?
Thank you very much
You need to check for error messages.
For debugging, comment out the header line and add these lines on the top to show all errors:
ini_set('display_errors',true);
error_reporting(E_ALL);
In many cases the error messages will tell you pretty clear whats wrong.

Overlay a PNG image (made in photoshop with transparency) on to a JPG using PHP

I have two images. I need to place one png image with some transparent layers on a jpeg.
This code SAVES the image in the dir as "merged.png" instead of showing it -
<?php
header("Content-Type: image/png");
$width = 200;
$height = 200;
$base_image = imagecreatefromjpeg("image.jpg");
$top_image = imagecreatefrompng("miod.png");
$merged_image = "merged.png";
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($base_image, $top_image, 100, 100, 100, 100, $width, $height);
imagepng($base_image, $merged_image);
?>
-How do I display it on the page itself?
imagepng has two main modes of operation. With a single parameter, it outputs the file directly, so it'll show up in the browser. With two parameters, as you have, it wills save the image from the first parameter into the file given in the second parameter.
So -- just drop your second parameter, and things should work fine:
imagepng($base_image);

Rotate image on top of background-image

I have two images. one is a jpg image of a rotated polaroid frame polaroid.jpg. The other is just an ordinary image image.jpg.
I'm trying to rotate the image, and then put it on top of the polaroid-image, and then show the merged images as one jpg-image.
I think I'm pretty close with the following code, but I can't manage to get the transparancy working. The uncovered zone of the rotated image is black instead of transparent. What am I doing wrong? I've added a comment to the lines that are relevant for getting a transparent background for the top-image.
$bg_src = "polaroid.jpg";
$img_src = "image.jpg";
$outputImage = imagecreatefromjpeg($bg_src);
$img = imagecreatefromjpeg($img_src);
// This should create transparent background.
$bgd_color = imagecolorallocatealpha($img, 0, 0, 0, 127);
// This should assign the transparent background to the uncovered zone after rotation
$img = imagerotate($img, 10, $bgd_color);
// This should make sure the alpha transparency gets saved
imagesavealpha($img, true);
$img_x = imagesx($img);
$img_y = imagesy($img);
imagecopymerge($outputImage,$img,156,50,0,0,$img_x,$img_y,100);
header('Content-type: image/jpeg');
imagejpeg($outputImage);
imagedestroy($outputImage);
Figured it out after some heave searching. Turns out to be real simple.
I just changed this line:
imagesavealpha($img, true);
to this:
imagecolortransparent($img,$bgd_color);
yay! :)

Using GD to change the color of a one color shape on a transparent background while preserving transparency

I have a png that is a set of white shape on a transparent background. I'm trying to change to color of the shapes while preserving the transparent background. I've been experimenting with the code below which does change the color but results in a black background. I think the imagetruecolortopalette is causing the problem but the color doesn't change if I remove that line.Any suggestions?
<?php
$imgname = "whiteim.png";
$im = imagecreatefrompng ($imgname);
imagetruecolortopalette($im,false, 255);
$index = imagecolorclosest ( $im, 255,255,255 ); // get White COlor
imagecolorset($im,$index,255,0,0); // SET NEW COLOR
$imgname = "result.png";
imagepng($im, $imgname ); // save image as png
imagedestroy($im);
?>
# imagecolortransparent($im, $xxxx); //not sure why this works
I think this work because imagecolortransparent makes the given color (where you placed $xxxx) transparent, in this case $xxxx contains no value. So what is made transparent are all the pixels that contain no color value.
One thing is I couldn't make that working using imagetruecolortopalette either. Not quite sure if you can use the imagefill function in your case (you need to know where to start the fill and it works if you have one area of white), but this is what I've used.
The other thing is that it seems like you need to call imagesavealpha before you save any alpha information to a png image, otherwise it's lost. Hard to tell for me why isn't it a default setting.
All in all, my approach was:
$imgname = "whiteim.png";.
$im = imagecreatefrompng ($imgname);
imagefill($im, 0,0, imagecolorallocate($im, 255,0,0));
$imgname = "result.png";
imagesavealpha($im, True);
imagepng($im, $imgname ); // save image as png
imagedestroy($im);

Add styled backgrounds to images in PHP

I am working on improving one of my Facebook apps by allowing the user to upload an image and apply a styled border, or frame to it (i.e. clouds, stars, sky etc). The user chould also be able to save the image, with the border after it has been applied. This explains a little better what I need:
http://zbrowntechnology.info/ImgDisp/imgdisp.php
If you have any other questions or need more details, please let me know.. I'll edit this post.
Use imagecopy(). The example on that page is done using the transparency option with imagecopymerge() but I don't think you need that.
Using imagecopy() you'll specify the X/Y coordinates to use for positioning:
imagecopy( $borderimage, $topimage, 20, 20, 0, 0, $width, $height);
Where $width and $height will be the entire width and height of the top image. You'll want to replace 20 and 20 with the measurement for how much of the border image will be showing around the borders. You will probably have to resize the top image to the exact dimensions you want, or else it might overlap the border a little too far to the right or bottom. (see imagecopyresampled())
Edit:
Here's a rough way to do the whole process (assuming chosenborder.png is the border they chose, and uploadedimage.png is the image they uploaded. If it's a different image type you'll use the corresponding function).
$borderx = 20; // The width of our border
$border = imagecreatefrompng("chosenborder.png");
$topimage = imagecreatefrompng("uploadedimage.png");
$bordersize = getimagesize($border);
$topimagesize = getimagesize($topimage);
/* The new dimensions of topimage. $borderx*2 means we account for
the border on both left and right, top and bottom. */
$newx = $bordersize[0] - ($borderx*2);
$newy = $bordersize[1] - ($borderx*2);
imagecopyresampled( $topimage_scaled, $topimage, 0, 0, 0, 0,
$newx, $newy, $topimagesize[0], $topimagesize[1]);
/* Merge the images */
imagecopy( $border, $topimage_scaled, $borderx, $borderx,
0, 0, $width, $height);
/* Output the image */
imagepng($border, "newimage.png");
/* Free up the memory occupied by the image resources */
imagedestroy($border);
imagedestroy($topimage);
After the user uploads their image, find chosenborder.png and uploadedimage.png, run the above script, then display newimage.png to the user and you're good to go. Just make sure you call imagedestroy() on the temporary image resources or they'll eat up memory.
If you don't want to keep the generated image on your server, you can omit the second argument to imagepng() which will make it send the image information directly as an image to the browser, in which case you'll want to write the correct image HTTP headers.
Client-side solution by using css3:
checkout the css3 property border-image
(dosen't meet the requirement of saving the img with the border)
Server-side solution by merging 2 different images:
<?php
$imgFile = 'img.jpg';
$brdFile = 'brd.jpg';
$img = addBorder($imgFile,$brdFile);
outputImage($img);
function addBorder($imgFile,$brdFile)
{
$img=imagecreatefromjpeg($imgFile);
$brd=imagecreatefromjpeg($brdFile);
$imgSize = getimagesize($imgFile);
$brdSize = getimagesize($brdFile);
//NOTE: the border img MUST be bigger then the src img
$dst_x = ceil(($brdSize[0] - $imgSize[0])/2);
$dst_y = ceil(($brdSize[1] - $imgSize[1])/2);
imagecopymerge ( $brd, $img, $dst_x, $dst_y, 0, 0, $imgSize[0], $imgSize[1] ,100 );
return $brd;
}
function outputImage($img)
{
header('Content-type: image/png');
imagepng($img);
}
?>

Categories