4

我正在尝试通过 php 函数IMG_FILTER_GRAYSCALE将 jpeg 图像转换为黑白(灰度)。它工作正常,但我想将图像保存到文件夹中。

代码下方:

$im = imagecreatefromjpeg('pathtomyimage/myimage.jpg');
if($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
 header('Content-Type: image/jpeg');
 imagejpeg($im);
} else 
 print 'Error during the b & w conversion';

毕竟很简单...

通过这种方式,它在屏幕上打印黑白图像,我在浏览器上看到它,但我无法将它保存到文件夹中(例如img/bw/myimage.jpg)。

有办法吗?我该怎么做?

4

3 回答 3

4

手册

imagejpeg($im, 'img/bw/myimage.jpg');
于 2013-12-20T10:43:03.980 回答
1

您可以通过这种方式使用 imagejpeg 将图像存储到文件夹中:

imagejpeg($image, "/path/to/store/file.jpg");
于 2013-12-20T10:49:22.400 回答
0

你可以这样使用

$tmp=imagecreatetruecolor($newwidth,$newheight);

    $newwidth1=120;
    $newheight1=150;
    $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
     $width,$height);

    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, 
    $width,$height);

    $filename = "../Advertisement/". $_FILES['img']['name'];
    $filename1 = "../Advertisement/small". $_FILES['img']['name'];

    $filename2 = $_FILES['img']['name'];
    imagejpeg($tmp,$filename,100);
    imagejpeg($tmp1,$filename1,100);

    imagedestroy($src);
    imagedestroy($tmp);
    imagedestroy($tmp1);
于 2013-12-20T10:46:48.763 回答