[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

php Image Functions

Posted on 2007-11-27
2
Medium Priority
?
280 Views
Last Modified: 2013-12-25
See sample.

The first image is a jpeg. I need to know the php image function coding to produce the 2nd image (with the green background) from the first.
0
Comment
Question by:Richard Korts
2 Comments
 
LVL 5

Expert Comment

by:mms_master
ID: 20361913
I don't know how to do this if the image is a jpeg sorry.
I'm not sure if its even possible. The only ways I can think of doing it with jpegs is to either replace the white colour for another colour; which could result in problems if there are other white ares in the image that shouldnt be replaced. Or to try to create a shape in the white areas and fill the shape with a new colour. But that means it would only fill that shape, and if you use a different image it wont work...

However if you can use gif images instead of jpeg images I have a solution for you.
Change your first image to a gif with a transparent background instead of a white background.
Then use the following php code
(Note: the output file is a gif, but it can be changed to jpeg as long as the first image is a gif)
<?php
 
function EditImage($image, $red, $green, $blue, $newfile) {
 
	$height = imagesy($image);
	$width = imagesx($image);
 
    $img = imagecreatetruecolor($width, $height);
    $col = imagecolorallocate($img, $red, $green, $blue);
	imagefill($img, 0, 0, $col);
	imagecopymerge($img, $image, 0, 0, 0, 0, $width, $height, 100);
	imagegif($img, $newfile);
	
 
}
 
header("Content-type: image/gif");
$NewImage = EditImage(imagecreatefromgif("merged4.gif"), 3, 108, 101, "newimage.gif");
echo "<html><body>";
echo "<img src='newimage.gif'>";
echo "</body></html>";
?>

Open in new window

0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 20366271
With white background it would be very very difficult to do.
Instead use the image i uploaded for you. Sort of like they use a green screen in the movies.

https://filedb.experts-exchange.com/incoming/ee-stuff/5859-merged4.jpg

and use the script below to change colors
<? header("Content-type:image/jpeg");
 
	$img = imagecreatefromjpeg("merged4.jpg"); // create image
	
	$width = imagesx($img); // get width
	
	$height = imagesy($img); // get height
	
	$color = imagecolorallocate($img,3,108,101); // allocate new color (rgb values)
	
	//$color = imagecolorallocate($img,255,120,0); //test with another color
	
	for($x=0;$x<$width;$x++) { //loop all x
	
		for($y=0;$y<$height;$y++) { //loop all y
		
			$set = imagecolorat($img,$x,$y); //get index for x,y
 
			$colors = imagecolorsforindex($img, $set); // get rgb
			
			if($colors["red"] < 15 && $colors["green"] > 240  && $colors["blue"] < 15	) { // check color 
			
				imagesetpixel($img,$x,$y,$color); //replace color
				
			}
		
		}
		
	}
 
	imagejpeg($img,null,100); //output image
	
?>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question