I'm using the following script to save images on a folder, but there is one image that shows this message "the image cannot be displayed because it contains errors".
I think the problem is something called cmyk and rgb on the image, but when searching on the web for a way so convert cmyk to rgb using php I can't find an example of how to do this.
Here is one of example: http://offshootinc.com/blog/2008/10/24/using-the-imagick-class-to-convert-a-cmyk-jpeg-to-rgb/ but I don't undestand how to use that in my case.
The link for the image is something like: www.example.com/attachment?id=2290
The script is as follow:
<?php
$image = 'http://www.dealrush.ie/attachment?id=2290';
$name = 'somename';
$alt = 'somealt';
$saveimage = file_get_contents($image);
file_put_contents("/usr/local/pem/vhosts/155030/webspace/httpdocs/img/$name.jpg", $saveimage);?>
Later in some pages I will use something like this to show the image.
<img src="http://www.example.com/img/<?php echo $name?>
.jpg" alt="<?php echo $alt?>
" height="127px" width="190px"/>
Any help with converting these images will be appreciated Thanks Daniel
I doubt that the colorspace (CMYK or RGB) is your problem. Although everyone should be using RGB images on the Net, the browsers will still display a CMYK image without complaint.
To convert the image from CMYK to RGB, you need to have an imagine manipulation program installed, such as ImageMagick, GraphicsMagick, or ExactImage. Any of these can do what you want, but have to be installed by the server admin. If you're luckly, ImageMagick might already be installed, in which case you could do this:
$image= '/path/to/your/file.jpg';
$i = new Imagick($image);
$i->setImageColorspace(Imagick::COLORSPACE_SRGB);
$i->writeImage($image);
$i->destroy();
Note that ImageMagick is the most powerful, ExactImage is the fastest, and GraphicsMagick is a folk of ImageMagick, which is faster but a little less powerful and has some bugs. Only ImageMagick can be used from PHP, the others have to be executed with the exec
function, although that is not necessarily a bad thing as they probably handle their own memory and cleanup much better than PHP would.