PHP 保存图像后图像复制重新采样


PHP Save Image After imagecopyresampled

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

如何将调整大小的图像保存到文件夹/?如何检测图像类型是 jpg/png/gif?

要将图像保存到文件,您可以使用以下任何一种:imagejpeg((,imagepng((或imagegif((,具体取决于所需的输出格式。

要检测图像类型,您只需检查文件的扩展名并以此为基础。但是,有时人们手动更改图像文件的扩展名,认为这实际上会更改图像类型,因此检查 imagecreatefrom 是否返回图像资源而不是 false 总是一个好主意。

对于仅返回文件扩展名的快速方法:

$ext = pathinfo($path_to_file, PATHINFO_EXTENSION);

在路径信息(( 上手动输入

添加此代码

imagepng($iOut,'pic/mypic.png',3);

和这个代码从外部来源获取你的图片格式

$link='http://example.com/example.png';
echo (substr ($link,strrpos ($link,".")+1));

您可以定义任何类型的图像:

 // Save the image as 'simpletext.jpg'
 imagejpeg($im, 'path/to/your/image.jpg');
 // or another image
 imagepng($im, 'path/to/your/image.png');

请参阅此处的示例 http://php.net/manual/en/function.imagecopyresampled.php

$filename = 'path/to/original/file.xxx'; // where xxx is file type (jpg, gif, or png)
$newfilename = 'path/to/resized/file.xxx'; // where xxx is file type (jpg, gif, or png)
$path_parts = pathinfo($filename);
if ($path_parts['extension'] == 'jpg') {
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, $newfilename);
} elseif ($path_parts['extension'] == 'gif') {
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromgif($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagegif($image_p, $newfilename);
} elseif ($path_parts['extension'] == 'png') {
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefrompng($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagepng($image_p, $newfilename);
} else {
        echo "Source file is not a supported image file type.";
}

应用 imagecopyresampled() 后,$dst_image将是图像资源标识符。

仅应用 imagecopyresampled() 函数不会自动将其保存到文件系统。

因此,您需要使用imagejpeg()imagepng()

// Output
imagejpeg($dst_image, 'new-image.jpg', 100);

要将图像另存为 JPG,请参阅图像jpeg函数

http://nz.php.net/manual/en/function.imagejpeg.php

获取图像扩展程序的使用

$path_parts = pathinfo($filename);
echo $path_parts['extension'];