image processing in php

converting a color image to gray scale

Page 1 of 1

2 Replies - 1635 Views - Last Post: 17 May 2008 - 04:36 AM Rate Topic: -----

#1 mitra_rub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-October 07

image processing in php

Posted 17 May 2008 - 03:11 AM

hello every body
I try to convert an image to grayscale but there is some thing wrong with my program when my when my image has capasity about 1 MG :angry: that i see a time limite exception on the bowser.
and here is my code: :v:


<?php
	// replace with your files
	$originalFileName	= "colorPicture.jpg";
	$destinationFileName = "bwPicture.jpg";
	
	// create a copy of the original image
	// works with jpg images
	// fell free to adapt to other formats;)
	$fullPath = explode(".",$originalFileName);
	$lastIndex = sizeof($fullPath) - 1;
	$extension = $fullPath[$lastIndex];
	if (preg_match("/jpg|jpeg|JPG|JPEG/", $extension)){
		$sourceImage = imagecreatefromjpeg($originalFileName);
	}

	// get image dimensions
	$img_width  = imageSX($sourceImage);
	$img_height = imageSY($sourceImage);

	// convert to grayscale
	// note: this will NOT affect your original image, unless
	// originalFileName and destinationFileName are the same
	for ($y = 0; $y <$img_height; $y++) { 
		for ($x = 0; $x <$img_width; $x++) { 
			$rgb = imagecolorat($sourceImage, $x, $y);
			$red   = ($rgb >> 16) & 0xFF;
			$green = ($rgb >> 8)  & 0xFF;
			$blue  = $rgb & 0xFF;

			$gray = round(.299*$red + .587*$green + .114*$blue);
			
			// shift gray level to the left
			$grayR = $gray << 16;   // R: red
			$grayG = $gray << 8;	// G: green
			$grayB = $gray;		 // B: blue
			
			// OR operation to compute gray value
			$grayColor = $grayR | $grayG | $grayB;

			// set the pixel color
			imagesetpixel ($sourceImage, $x, $y, $grayColor);
			imagecolorallocate ($sourceImage, $gray, $gray, $gray);
		}
	}

	// copy pixel values to new file buffer
	$destinationImage = ImageCreateTrueColor($img_width, $img_height);
	imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);

	// create file on disk
	imagejpeg($destinationImage, $destinationFileName);
	imagejpeg($sourceImage, $fullPath . "/output/"  . ".jpg", 100);
	// destroy temp image buffers
	imagedestroy($destinationImage);	
	imagedestroy($sourceImage);
	
?>


i am waiting for your help

thanx


** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: image processing in php

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: image processing in php

Posted 17 May 2008 - 03:18 AM

View Postmitra_rub, on 17 May, 2008 - 06:11 AM, said:

I try to convert an image to grayscale but there is some thing wrong with my program when my when my image has capasity about 1 MG :angry: that i see a time limite exception on the bowser.

What browser are you using?
Was This Post Helpful? 0
  • +
  • -

#3 mitra_rub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-October 07

Re: image processing in php

Posted 17 May 2008 - 04:36 AM

View Postno2pencil, on 17 May, 2008 - 03:18 AM, said:

View Postmitra_rub, on 17 May, 2008 - 06:11 AM, said:

I try to convert an image to grayscale but there is some thing wrong with my program when my when my image has capasity about 1 MG :angry: that i see a time limite exception on the bowser.

What browser are you using?

internet explorer (ie)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1