PHP 在 imagecopyresampled 后保存图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9452650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Save Image After imagecopyresampled
提问by JBL
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
How can i save the resized image to folder/ ? And how can I detect the image type is jpg/png/gif?
如何将调整后的图像保存到文件夹/?以及如何检测图像类型是 jpg/png/gif?
回答by MichD
To save the image to a file, you can use any of these: imagejpeg(), imagepng(), or imagegif(), depending on your desired output format.
要将图像保存到文件中,您可以使用以下任何一种:imagejpeg()、imagepng()或imagegif(),具体取决于所需的输出格式。
To detect the image type, you can just check the file's extension and base yourself on that. However, sometimes people manually change the extension of an image file, thinking that actually changes the image type, so it's always a good idea to check whether imagecreatefrom returned an image resource rather than false.
要检测图像类型,您只需检查文件的扩展名并以此为基础。但是,有时人们会手动更改图像文件的扩展名,认为实际上更改了图像类型,因此检查 imagecreatefrom 是否返回图像资源而不是 false 总是一个好主意。
For a quick way to return just the extension of a file:
一种快速返回文件扩展名的方法:
$ext = pathinfo($path_to_file, PATHINFO_EXTENSION);
回答by Pooya Estakhri
add this code
添加此代码
imagepng($iOut,'pic/mypic.png',3);
& this code to get your pics format from an external source
&此代码可从外部来源获取您的图片格式
$link='http://example.com/example.png';
echo (substr ($link,strrpos ($link,".")+1));
回答by zoonman
You can define any type image:
您可以定义任何类型的图像:
// Save the image as 'simpletext.jpg'
imagejpeg($im, 'path/to/your/image.jpg');
// or another image
imagepng($im, 'path/to/your/image.png');
See examples here http://php.net/manual/en/function.imagecopyresampled.php
请参阅此处的示例http://php.net/manual/en/function.imagecopyresampled.php
回答by zoonman
$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.";
}
回答by Valentin Despa
After you applied imagecopyresampled()
, the $dst_image
will be your image resource identifier.
应用后imagecopyresampled()
,$dst_image
将成为您的图像资源标识符。
Just applying the imagecopyresampled()
function does not automagically also save it to the file system.
仅应用该imagecopyresampled()
功能并不会自动将其保存到文件系统中。
So you will need to save it, using one of the functions imagejpeg()
, imagepng()
因此,您需要使用以下功能之一保存它imagejpeg()
,imagepng()
// Output
imagejpeg($dst_image, 'new-image.jpg', 100);
回答by bumperbox
to save image as jpg see imagejpeg function
将图像保存为 jpg 参见 imagejpeg 函数
http://nz.php.net/manual/en/function.imagejpeg.php
http://nz.php.net/manual/en/function.imagejpeg.php
to get image extension use
获取图像扩展使用
$path_parts = pathinfo($filename);
echo $path_parts['extension'];