Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I swap colors in image using GD library in PHP?

I got the image like this (it's a graph):

Gold Trade Graph
(source: kitconet.com)

I want to change the colours, so the white is black, the graph line is light blue, etc.. is it possible to achieve with GD and PHP?

like image 365
Skuta Avatar asked Jan 18 '09 22:01

Skuta


People also ask

How to manipulate images in PHP using GD?

The first step is to create an image resource with functions such as imagecreatefrompng() . After that, you can use the function imagecolorat($image, $x, $y) to get the index of the color of a given pixel. The position of the pixel is determined by the second and third parameters in the function.

Which type of pictures can be created by using GD library?

GD is an open source code library for the dynamic creation of images. GD is used for creating PNG, JPEG and GIF images and is commonly used to generate charts, graphics, thumbnails on the fly.

Which library is used in PHP to do various types of image work?

Zebra Image This is a compact (one-file only), lightweight, object-oriented image manipulation library written in and for PHP, that provides methods for performing several types of image manipulation operations.


1 Answers

This will replace the white color with Gray

$imgname = "test.gif";
$im = imagecreatefromgif ($imgname);

$index = imagecolorclosest ( $im,  255,255,255 ); // get White COlor
imagecolorset($im,$index,92,92,92); // SET NEW COLOR

$imgname = "result.gif";
imagegif($im, $imgname ); // save image as gif
imagedestroy($im);

enter image description here

like image 75
Luis Melgratti Avatar answered Sep 30 '22 02:09

Luis Melgratti