如何使用PHP将任何图像转换为JPG或JPEG
可能重复: 我正在将任何图像格式转换为JPEG或JPG。用于将图像转换为jpg的代码为 之后,我调整转换后的图像的大小,其代码为 在每一张图像上,它都会报告错误和警告如何使用PHP将任何图像转换为JPG或JPEG,php,gdlib,Php,Gdlib,可能重复: 我正在将任何图像格式转换为JPEG或JPG。用于将图像转换为jpg的代码为 之后,我调整转换后的图像的大小,其代码为 在每一张图像上,它都会报告错误和警告 请帮我解决那个问题。我必须完成它。不要使用扩展名来确定文件类型。若要检查给定文件是否为图像,并且其格式是否允许GD对其进行操作,请使用“不使用扩展名”确定文件类型。要检查给定文件是否为图像,并且其格式是否允许GD对其进行操作,请使用 imagecreatefromstring检测图像类型并创建图像资源,否则将引发异常 i
请帮我解决那个问题。我必须完成它。不要使用扩展名来确定文件类型。若要检查给定文件是否为图像,并且其格式是否允许GD对其进行操作,请使用“不使用扩展名”确定文件类型。要检查给定文件是否为图像,并且其格式是否允许GD对其进行操作,请使用 imagecreatefromstring检测图像类型并创建图像资源,否则将引发异常
imagecreatefromstring检测图像类型并创建图像资源,否则将引发异常。不确定gd扩展。试试Imagick。使用@来抑制错误从来都不是一个好主意,尤其是当您试图弄清楚脚本为什么不能按预期工作时。不确定gd扩展。试试Imagick。使用@来抑制错误从来都不是一个好主意,尤其是当您试图找出脚本未按预期工作的原因时。使用@来抑制错误从来都不是一个好主意,尤其是当您试图找出脚本未按预期工作的原因时。使用@来抑制错误从来都不是一个好主意,尤其是当你试图弄清楚为什么脚本不能按预期工作时。我不能同意。请参阅第节。当然,对于非图像文件或无法识别的图像格式,这不会有多大帮助,但更常见的情况是使用png扩展名保存的jpeg文件。不同意。当然,对于非图像文件或无法识别的图像格式,这并没有多大帮助,但更常见的情况是使用png扩展名保存的jpeg文件。
function converttojpg($originalimg)
{
$filetype = @end(explode(".", $originalimg));
echo $originalimg . " asdasdfs";
if (strtolower($filetype) == 'jpg' or strtolower($filetype) == 'jpeg')
{
$srcImg = imagecreatefromjpeg($originalimg);
imagejpeg($srcImg,$originalimg,100);
imagedestroy($srcImg);
}
if (strtolower($filetype) == 'png')
{
$srcImg = imagecreatefrompng($originalimg);
imagejpeg($srcImg,$originalimg,100);
imagedestroy($srcImg);
}
if (strtolower($filetype) == 'gif')
{
echo "executed";
$srcImg = imagecreatefromgif($originalimg);
imagejpeg($srcImg,$originalimg,100);
imagedestroy($srcImg);
}
}
function resizeImage($originalImage,$toWidth,$toHeight)
{
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
// Recalculate new size with default ratio
if ($yscale>$xscale)
{
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else
{
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
return $imageResized;
}
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9 in E:\xampp\htdocs\photo\functions.php on line 33
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'userphotos/corrupted_image_1.jpg' is not a valid JPEG file in E:\xampp\htdocs\photo\functions.php on line 33
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 34
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 35
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9 in E:\xampp\htdocs\photo\functions.php on line 22
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'userphotos/53.jpg' is not a valid JPEG file in E:\xampp\htdocs\photo\functions.php on line 22
Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 23
userphotos/MG_diesel-04.gif_thumb.png asdasdfsuserphotos/Porsche_911_996_Carrera_4S.jpg asdasdfs
<?php
$image = @imagecreatefromstring(file_get_contents($file_path));
if ($image === false) {
throw new Exception (sprintf('invalid image or file not found, file=%s', $file_path));
}
?>