在上载php时调整图像大小
我有一个php脚本的图像上传如下在上载php时调整图像大小,php,image-processing,file-upload,Php,Image Processing,File Upload,我有一个php脚本的图像上传如下 <?php $LibID = $_POST[name]; define ("MAX_SIZE","10000"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l);
<?php
$LibID = $_POST[name];
define ("MAX_SIZE","10000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg"))
{
echo '<h1>Unknown extension!</h1>';
$errors=1;
exit();
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
exit();
}
$image_name=$LibID.'.'.$extension;
$newname="uimages/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>image upload unsuccessfull!</h1>';
$errors=1;
exit();
}}}
?>
它将图像文件上载到根目录中的文件夹“uimages”。通过定义“最大高度”和“最大宽度”,我在html文件中对图像的紧凑显示进行了更改。但我想在上传时调整图像文件的大小。图像文件的最大宽度为100px,最大高度为150px。必须约束图像比例。也就是说,图像可能小于上述尺寸,但不应超过限制。我怎样才能做到这一点
提前感谢:)
blasteralfred..看,我喜欢自己使用imagemagick看,我喜欢自己使用imagemagick上传时无法调整图像大小;它必须首先在服务器上
为此,您可以使用ImageMagick查看。如果您使用的是GD库,那么您可以使用。上传时无法调整图像大小;它必须首先在服务器上
为此,您可以使用ImageMagick查看。如果您正在使用GD库,则可以使用。您可以使用PHPs本机GD库调整图像大小。这里有一个链接,指向我编写的用于将图像大小调整为任意大小的函数。它有字母框或裁剪选项,以适应新的纵横比,并有一个很好的解释
您可以使用PHPs本机GD库调整图像大小。这里有一个链接,指向我编写的用于将图像大小调整为任意大小的函数。它有字母框或裁剪选项,以适应新的纵横比,并有一个很好的解释
上传后,您可以使用2个功能调整图像大小
<?php
function thumbnail($inputFileName, $maxSize = 100) {
$info = getimagesize($inputFileName);
$type = isset($info['type']) ? $info['type'] : $info[2];
if (!(imagetypes() & $type)) {
return false;
}
$width = isset($info['width']) ? $info['width'] : $info[0];
$height = isset($info['height']) ? $info['height'] : $info[1];
// Calculate aspect ratio
$wRatio = $maxSize / $width;
$hRatio = $maxSize / $height;
// Using imagecreatefromstring will automatically detect the file type
$sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
// Calculate a proportional width and height no larger than the max size.
if (($width <= $maxSize) && ($height <= $maxSize)) {
// Input is smaller than thumbnail, do nothing
return $sourceImage;
} elseif (($wRatio * $height) < $maxSize) {
// Image is horizontal
$tHeight = ceil($wRatio * $height);
$tWidth = $maxSize;
} else {
// Image is vertical
$tWidth = ceil($hRatio * $width);
$tHeight = $maxSize;
}
$thumb = imagecreatetruecolor($tWidth, $tHeight);
if ($sourceImage === false) {
// Could not load image
return false;
}
// Copy resampled makes a smooth thumbnail
imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
imagedestroy($sourceImage);
return $thumb;
}
function imageToFile($im, $fileName, $quality = 80) {
if (!$im || file_exists($fileName)) {
return false;
}
$ext = strtolower(substr($fileName, strrpos($fileName, '.')));
switch ($ext) {
case '.gif':
imagegif($im, $fileName);
break;
case '.jpg':
case '.jpeg':
imagejpeg($im, $fileName, $quality);
break;
case '.png':
imagepng($im, $fileName);
break;
case '.bmp':
imagewbmp($im, $fileName);
break;
default:
return false;
}
return true;
}
?>
打这样的电话:
$image=缩略图($uploadedFile,300);
imageToFile($image$folder);//如果要保存缩略图,可以在上载后使用2个函数调整图像大小
<?php
function thumbnail($inputFileName, $maxSize = 100) {
$info = getimagesize($inputFileName);
$type = isset($info['type']) ? $info['type'] : $info[2];
if (!(imagetypes() & $type)) {
return false;
}
$width = isset($info['width']) ? $info['width'] : $info[0];
$height = isset($info['height']) ? $info['height'] : $info[1];
// Calculate aspect ratio
$wRatio = $maxSize / $width;
$hRatio = $maxSize / $height;
// Using imagecreatefromstring will automatically detect the file type
$sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
// Calculate a proportional width and height no larger than the max size.
if (($width <= $maxSize) && ($height <= $maxSize)) {
// Input is smaller than thumbnail, do nothing
return $sourceImage;
} elseif (($wRatio * $height) < $maxSize) {
// Image is horizontal
$tHeight = ceil($wRatio * $height);
$tWidth = $maxSize;
} else {
// Image is vertical
$tWidth = ceil($hRatio * $width);
$tHeight = $maxSize;
}
$thumb = imagecreatetruecolor($tWidth, $tHeight);
if ($sourceImage === false) {
// Could not load image
return false;
}
// Copy resampled makes a smooth thumbnail
imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
imagedestroy($sourceImage);
return $thumb;
}
function imageToFile($im, $fileName, $quality = 80) {
if (!$im || file_exists($fileName)) {
return false;
}
$ext = strtolower(substr($fileName, strrpos($fileName, '.')));
switch ($ext) {
case '.gif':
imagegif($im, $fileName);
break;
case '.jpg':
case '.jpeg':
imagejpeg($im, $fileName, $quality);
break;
case '.png':
imagepng($im, $fileName);
break;
case '.bmp':
imagewbmp($im, $fileName);
break;
default:
return false;
}
return true;
}
?>
打这样的电话:
$image=缩略图($uploadedFile,300);
imageToFile($image$folder);//如果要保存缩略图问题的可能副本已关闭。。这里更新的问题:抱歉,删除将使所有试图帮助您的人的工作无效。请选择对您最有帮助的答案。关于你为什么离开这个问题的评论也很好。这个问题的可能重复部分已经结束。。这里更新的问题:抱歉,删除将使所有试图帮助您的人的工作无效。请选择对您最有帮助的答案。关于你为什么离开这个问题的评论也不错。