Php 将图像大小脚本展开为.PNG和.GIF
我有一个脚本可以完美地调整.JPG图像的大小,它完全按照它应该做的做。 但是,我想为用户提供添加.PNG en.GIF图像的选项。有人能帮我扩展下面的脚本,使它也支持这些格式吗?提前谢谢Php 将图像大小脚本展开为.PNG和.GIF,php,image,resize,gif,Php,Image,Resize,Gif,我有一个脚本可以完美地调整.JPG图像的大小,它完全按照它应该做的做。 但是,我想为用户提供添加.PNG en.GIF图像的选项。有人能帮我扩展下面的脚本,使它也支持这些格式吗?提前谢谢 $image = $_POST['bedrijfslogo']; $new_image = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg"; copy($_POST['bedrijfsl
$image = $_POST['bedrijfslogo'];
$new_image = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg";
copy($_POST['bedrijfslogo'],"copylogo.jpg");
$width=250; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($image);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($image);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin, $new_image);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
unlink($image);
根据所需输出文件的类型,添加并选择:
imagecreatefrompng
及
及
及
在我的评论之后,我意识到您可以从
getimagesize()
返回的数据中提取图像的类型
试试看,它应该接受PHP实例能够理解的任何类型的图像(未测试):
现在,这段代码似乎是为处理本地源映像而设计的,但是您使用它的方式表明您可能从用户那里获取URL并下载远程映像。如果是这种情况,请让我知道,我将对此稍作修改,以更好地适应这一目的。变化是什么
$images_orig = ImageCreateFromJPEG($image); //for jpg
您可以查看文档
您也可以通过和
希望这有助于获取上传文件的MIME类型,
切换它,并根据MIME类型使用imagecreatefrom*()
为$images\u orig
指定值。或者,如果您感到懒惰,您可以安全地(但不太可靠地)使用文件扩展名来确定要使用哪个函数。除此之外,脚本的其余部分可以保持不变。谢谢你的提示,但是这个脚本不是我的。我在网上找到了它,可以根据我的需要进行编辑。我不是PHP专业人士,所以请多给我一点帮助:在脚本的最后一部分,我该如何处理ImageJPEG?这应该很有效,今天将对其进行测试。非常感谢达维兰多姆。用户可以将照片添加到他们的公司档案中,如果照片符合te要求,则会上载到临时文件夹。我们(管理员)必须在处理他们的配置文件之前进行批准,因此在我们的批准页面上,我有几个输入字段,其中的值为“temp folder filepath”。因此,我使用的是本地文件,而不是直接来自用户。非常感谢!不幸的是,它没有像我们希望的那样工作。当我使用JPG时,我没有得到一个错误,但它会按预期删除srcfile,但不会在指定的文件夹中创建一个新文件。如果我上传一个.PNG文件,它只会声明默认的“未知输入文件类型”。当我用我的旧脚本做这两件事时,它确实适用于.JPG,但是如果我使用.PNG,我只会得到一个黑色的图像。谢谢@user1555076但不在指定的文件夹中创建新文件。
-这是我的错误,现已在上述代码中更正<代码>如果我上传一个.PNG文件,它只会声明默认的“未知输入文件类型”
-我已经编辑了上面的代码,因此它会显示一些更有用的调试信息-你能再试一次并将你得到的输出粘贴到这里吗?结果是srcType与错误的常量进行了比较。它应该与IMAGETYPE_XXX
常量进行比较,而不是与IMG_XXX
常量进行比较。如果您将所有案例更新为IMAGETYPE\u XXX
(例如IMAGETYPE\u PNG
),它应该可以工作。
imagepng
imagegif
// Get file paths
// Taking a local file path direct from the user? This is a *big* security risk...
$srcPath = $_POST['bedrijfslogo'];
$dstPath = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg";
// Not really sure why this is here, you don't seem to use the copy of the file
copy($srcPath, "copylogo.jpg");
// Get information about source image
$info = getimagesize($srcPath);
$srcWidth = $info[0];
$srcHeight = $info[1];
$srcType = $info[2];
// Calculate new width and height
// Width is fixed, height calculated from width
$dstWidth = 250;
$dstHeight = round($dstWidth * $srcHeight / $srcWidth);
// Get the image types supported by this instance of PHP
$supportedTypes = imagetypes();
// Create the source image resource based on it's type
switch ($srcType) {
case IMG_GIF:
if ($supportedTypes & IMG_GIF) {
$srcImage = imagecreatefromgif($srcPath);
} else {
// GIF support not enabled on your PHP instance. Handle error here.
exit('GIF support not enabled on this PHP instance');
}
break;
case IMG_JPG: case IMG_JPEG: // Think these might have the same value?
if ($supportedTypes & IMG_JPG) {
$srcImage = imagecreatefromjpeg($srcPath);
} else {
// JPEG support not enabled on your PHP instance. Handle error here.
exit('JPEG support not enabled on this PHP instance');
}
break;
case IMG_PNG:
if ($supportedTypes & IMG_PNG) {
$srcImage = imagecreatefrompng($srcPath);
} else {
// PNG support not enabled on your PHP instance. Handle error here.
exit('PNG support not enabled on this PHP instance');
}
break;
case IMG_WBMP:
if ($supportedTypes & IMG_WBMP) {
$srcImage = imagecreatefromwbmp($srcPath);
} else {
// WBMP support not enabled on your PHP instance. Handle error here.
exit('WBMP support not enabled on this PHP instance');
}
break;
case IMG_XPM:
if ($supportedTypes & IMG_XPM) {
$srcImage = imagecreatefromxpm($srcPath);
} else {
// XPM support not enabled on your PHP instance. Handle error here.
exit('XPM support not enabled on this PHP instance');
}
break;
default:
// Unknown input file type. Handle error here. Currently prints some debugging info
echo 'Unknown input file type';
var_dump($info);
exit;
}
// Create the destination image resource
$dstImage = imagecreatetruecolor($dstWidth, $dstHeight);
// Resample source image onto destination image
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $dstWidth + 1, $dstHeight + 1, $srcWidth, $srcHeight);
// Save new image to disk
imagejpeg($dstImage, $dstPath);
// Destroy resources
imagedestroy($srcImage);
imagedestroy($dstImage);
$srcImage = $dstImage = NULL;
// Remove source image - are you sure you want to do this?
unlink($srcPath);
$images_orig = ImageCreateFromJPEG($image); //for jpg