如何在上传到dir之前调整图像大小(使用来自网址的图片)?
我想将图片大小调整为200x200 px
,我该怎么做?
我尝试将图片从网址上传到目录upload
,这样做很好。
但是现在我想在上传之前调整图片大小,我该怎么做?
<?php
if($_POST){
$url = $_POST['url'];
$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;
$upload = file_put_contents("uploads/$name",file_get_contents($url));
if($upload) echo "Success: <a href='uploads/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}
?>
<html>
<body>
<h3>File Upload from URL Script!</h3>
<form action="" method="post">
Your URL: <input type="text" name="url" />
</form>
</body>
</html>
答案 0 :(得分:0)
您可以使用一些api来调整图像大小。 link
答案 1 :(得分:0)
首先在表单标签中使用enctype =&#34; multipart / form-data&#34;用于上传图片。
而不是运行这个脚本希望这会麻烦你。
if ($_POST)
{
$tmpname = $_FILES['image']['tmp_name'];
@img_resize( $tmpname , 600 , "uploads" , "album_".$id.".jpg");
@img_resize( $tmpname , 120 , "uploads" , "album_".$id."_small.jpg");
@img_resize( $tmpname , 60 , "uploads" , "album_".$id."_maxheight.jpg", 1);
}
else
echo "No Images uploaded via POST";
function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight = 0 )
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = getimagesize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
$x = imagesx($imorig);
$y = imagesy($imorig);
$woh = (!$maxisheight)? $gis[0] : $gis[1] ;
if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight){
$aw = $size;
$ah = $size * $y / $x;
} else {
$aw = $size * $x / $y;
$ah = $size;
}
}
$im = imagecreatetruecolor($aw,$ah);
if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y))
if (imagejpeg($im, $save_dir.$save_name))
return true;
else
return false;
}
答案 2 :(得分:0)
在表单标记
中使用enctype =“multipart / form-data”我希望这个脚本可以帮到你。
$path = getcwd();
$image_name = $_FILES["file"]["tmp_name"]; // your url image name
function resize($path , $image_name)
{
$src = $image_name; // your url image name
$dest = "upload/".$image_name; // your image upload folder name
$size = getimagesize($src);
switch($size["mime"])
{
case "image/jpeg":
$source_image = imagecreatefromjpeg($src);
break;
case "image/gif":
$source_image = imagecreatefromgif($src);
break;
case "image/png":
$source_image = imagecreatefrompng($src);
break;
case "image/jpg":
$source_image = imagecreatefromjpeg($src);
break;
default :
$source_image = false;
break;
}
$width = imagesx($source_image);
$height = imagesy($source_image);
/*$newwidth = 336;
$newheight = 195;*/
$newwidth = 30;
$newheight = 30;
$vertual_image = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($vertual_image,$source_image,0,0,0,0,$newwidth,$newheight,$width,$height);
$rs = imagejpeg($vertual_image,$dest,100);
}
resize($path , $image_name);
答案 3 :(得分:0)
制作此功能
<?php
function resize($width, $height, $imgPath, $nm){
/* Get original image x y*/
list($w, $h) = getimagesize($_FILES[$nm]['tmp_name']);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* new file name */
$path = $imgPath;
/* read binary data from image file */
$imgString = file_get_contents($_FILES[$nm]['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
//$nm = imagecreatetruecolor(400, 300);
imagealphablending( $tmp, FALSE );
imagesavealpha( $tmp, TRUE );
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($_FILES[$nm]['type']) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 0);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
?>
然后在上传文件之前编写以下代码
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
// thumbnail sizes
$sizes = array(200 => 200);
$ext = strtolower(pathinfo($_FILES[$nm]['name'], PATHINFO_EXTENSION));
if (in_array($ext, $valid_exts)) {
/* resize image */
foreach ($sizes as $w => $h) {
$files[] = resize($w, $h, $imgPath ,$nm);
}
}