I'm trying to convert an jpg image to grayscale with GD imagefilter but i can't get the filtered image to output on the browser. I use this code

<?php 
$thumb="w=250&q=100";
$imgstring = get_image('cover_image',1,1,0,NULL,$thumb);
$im = imagecreatefromjpeg($imgstring);
imagefilter($im, IMG_FILTER_GRAYSCALE);
header("Content-type: image/jpg");
imagejpeg($im);
imagedestroy($im);
?>

**The $imgstring is the url string of the image ('http://xxx.xx/image.jpg').

With the header content line the result is an error "Warning: Cannot modify header information - headers already sent by..." followed by what appears to be the image code (a lot of question marks characters and other garbage).

Without the header content line the result is just the image code(?) again (symbols and characters)

What's wrong?

有帮助吗?

解决方案

The error is exactly what it says - something in your code has caused output to be performed, which kills the header() call, preventing the content-type from being set. In the absence of any valid content-type header, the client browser assumes text/plain, and is showing your JPEG image - as raw "garbage".

You need to figure out WHERE in your code that output is being performed (I'm willing to bet a shiny penny it's in that get_image() function, and eliminate it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top