如何设置上传图像PHP的宽度+高度 [英] How to set width+height to uploaded image PHP
本文介绍了如何设置上传图像PHP的宽度+高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是保存上传的图片并设置宽度+高度。使用下面的代码我可以保存图像,但无法设置宽度+高度...请查看//和//之间的代码以发现错误。非常感谢。
my aim is to save uploaded image and set width+height to it. With the code below I can save the image, but cannot set width + height... Please have a look at the code between // and // to spot the mistakes. Thanks a lot.
$format = $_FILES["picture"]["type"];
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" .time(). $_FILES["picture"]["name"];
////////////////////////////////////////////
$_destination = width [400];
$_destination = height [460]; // this doesn't work... please help:)
//////////////////////////////////////////
move_uploaded_file($filename, $destination); //moves image to "upload" folder
$_SESSION['picture'] = $destination; //saves image path...
$_SESSION['format'] = $format; //saves image format
推荐答案
如果仅在服务器上安装GD后,您可以使用以下功能:
If on the server is only GD installed, you can use the following functions:
//helper function to resize an image based on input, output and size
function ResizeImage($input, $output, $mode, $w, $h = 0)
{
switch(GetMimeType($input))
{
case "image/png":
$img = ImageCreateFromPng($input);
break;
case "image/gif":
$img = ImageCreateFromGif($input);
break;
case "image/jpeg":
default:
$img = ImageCreateFromJPEG ($input);
break;
}
$image['sizeX'] = imagesx($img);
$image['sizeY'] = imagesy($img);
switch ($mode){
case 1: //Quadratic Image
$thumb = imagecreatetruecolor($w,$w);
if($image['sizeX'] > $image['sizeY'])
{
imagecopyresampled($thumb, $img, 0,0, ($w / $image['sizeY'] * $image['sizeX'] / 2 - $w / 2),0, $w,$w, $image['sizeY'],$image['sizeY']);
}
else
{
imagecopyresampled($thumb, $img, 0,0, 0,($w / $image['sizeX'] * $image['sizeY'] / 2 - $w / 2), $w,$w, $image['sizeX'],$image['sizeX']);
}
break;
case 2: //Biggest side given
if($image['sizeX'] > $image['sizeY'])
{
$thumb = imagecreatetruecolor($w, $w / $image['sizeX'] * $image['sizeY']);
imagecopyresampled($thumb, $img, 0,0, 0,0, imagesx($thumb),imagesy($thumb), $image['sizeX'],$image['sizeY']);
}
else
{
$thumb = imagecreatetruecolor($w / $image['sizeY'] * $image['sizeX'],$w);
imagecopyresampled($thumb, $img, 0,0, 0,0, imagesx($thumb),imagesy($thumb), $image['sizeX'],$image['sizeY']);
}
break;
case 3; //Both sides given (cropping)
$thumb = imagecreatetruecolor($w,$h);
if($h / $w > $image['sizeY'] / $image['sizeX'])
{
imagecopyresampled($thumb, $img, 0,0, ($image['sizeX']-$w / $h * $image['sizeY'])/2,0, $w,$h, $w / $h * $image['sizeY'],$image['sizeY']);
}
else
{
imagecopyresampled($thumb, $img, 0,0, 0,($image['sizeY']-$h / $w * $image['sizeX'])/2, $w,$h, $image['sizeX'],$h / $w * $image['sizeX']);
}
break;
case 0:
$thumb = imagecreatetruecolor($w,$w / $image['sizeX']*$image['sizeY']);
imagecopyresampled($thumb, $img, 0,0, 0,0, $w,$w / $image['sizeX']*$image['sizeY'], $image['sizeX'],$image['sizeY']);
break;
}
if(!file_exists($output)) imagejpeg($thumb, $output, 90);
}
//helper function to get the mime type of a file
function GetMimeType($file)
{
$forbiddenChars = array('?', '*', ':', '|', ';', '<', '>');
if(strlen(str_replace($forbiddenChars, '', $file)) < strlen($file))
throw new \ArgumentException("Forbidden characters!");
$file = escapeshellarg($file);
ob_start();
$type = system("file --mime-type -b ".$file);
ob_clean();
return $type;
}
然后在你的代码中使用它:
and then use it in your code:
//$filename: input filename
//$destination: output filename
//$mode: 1, quadratic / 2, proportional / 3, cropped
//$width: output image width
//$height: optional output image height, only needed for mode 3
ResizeImage($filename, $destination, $mode, $width, $height);
编辑:关于功能的一点解释。
a little explanation about functions.
<?php
function DisplayText($text)
{
echo $text;
}
function MultiplyNumbers($a, $b)
{
return $a*$b;
}
$test = MultiplyNumbers(3, 6);
DisplayText($test);
- >输出: 18
这篇关于如何设置上传图像PHP的宽度+高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文