【问题标题】:How to resize image before upload to dir (using image from url)?如何在上传到目录之前调整图像大小(使用来自 url 的图像)?
【发布时间】:2014-10-31 17:44:29
【问题描述】:

如何在上传到目录之前调整图像大小(使用来自 url 的图像)?

我想将图像大小调整为200x200 px,我该怎么做?

我尝试将图片从 url 上传到目录 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>

【问题讨论】:

  • @Ohgodwhy not duplicate,我的函数使用来自 url 的图像。
  • 由于您正在尝试调整从 URL 获得的图像的大小,因此您尝试将它们保存在某个临时文件夹中,运行调整大小功能,然后将其移动到适当的位置(无论您想要什么),然后删除临时一。
  • @WisdmLabs 你是对的。

标签: javascript php html image file-upload


【解决方案1】:

您可以使用一些 api 来调整图像大小。 link

【讨论】:

  • 我使用来自 url 的图片,而不是在我的电脑上
【解决方案2】:

首先在表单标签中使用 enctype="multipart/form-data" 上传图片。

比运行这个脚本希望这会让你感到麻木。

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;
}

【讨论】:

    【解决方案3】:

    在表单标签中使用 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);
    

    【讨论】:

      【解决方案4】:

      制作此功能

      <?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);
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-14
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        • 2011-04-16
        • 2015-12-05
        • 1970-01-01
        相关资源
        最近更新 更多