writing new image data to a file after converting to grayscale php -
im using following code take image, convert grayscale, , save grayscale image:
$imagename = "$imagename"; // path uploaded color jpeg $im = imagecreatefromjpeg($imagename); $imgw = imagesx($im); $imgh = imagesy($im); ($i=0; $i<$imgw; $i++) { ($j=0; $j<$imgh; $j++) { $rgb = imagecolorat($im, $i, $j); $rr = ($rgb >> 16) & 0xff; $gg = ($rgb >> 8) & 0xff; $bb = $rgb & 0xff; $g = round(($rr + $gg + $bb) / 3); $val = imagecolorallocate($im, $g, $g, $g); imagesetpixel ($im, $i, $j, $val); } } $grayimgpath = "step2cache/".$saltname."_gray.jpg"; //$saltname randomly generated image name $fh = fopen($grayimgpath, 'w') or die("can't open file"); fwrite($fh, $im); fclose($fh);
the problem is, when access file, blank, 15 bytes.
what missing here?
you trying write file image handle not actual image data. try this:
imagejpeg($im, $grayimgpath);
Comments
Post a Comment