2014-10-31 3 views
1

dir에 업로드하기 전에 이미지의 크기를 조정하는 방법 (URL의 이미지 사용)?dir에 업로드하기 전에 이미지 크기를 조정하는 방법 (URL의 이미지 사용)?

내가 200x200 px에 이미지 크기를 조정하려는

, 내가 어떻게 할 수 있습니까?

나는이 작업 좋은, upload을 dir에 대한 URL에서 이미지를 업로드하려고합니다. 하지만 업로드하기 전에 이미지의 크기를 조정하려면 어떻게해야합니까?

<?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

(HTTP [또는 업로드하기 전에 PHP 크기 조정 이미지]의 가능한 중복 : //stackoverflow.com/questions/12757005/php-resize-image-on-or-before-upload) – Ohgodwhy

+0

@ Ohgodwhy 중복되지 않습니다. m y 함수는 URL의 이미지를 사용합니다. – srimamong

+0

URL에서 얻은 이미지의 크기를 조정하려고하기 때문에 임시 폴더에 저장 한 다음 크기 조정 기능을 실행 한 다음 원하는 위치 (원하는 위치)로 이동 한 다음 임시 폴더를 삭제하십시오. – WisdmLabs

답변

0

이미지 크기를 조정할 때 일부 api를 사용할 수 있습니다. 업로드 이미지 = "multipart/form-data"로 폼 태그 사용에 enctype 최초 link

+0

내 PC에있는 URL의 이미지를 사용하지 않습니다. – srimamong

0

.

그리고이 스크립트를 실행보다는

이 당신을 대마 바랍니다. 폼 태그

에서 = "다중/폼 데이터"

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

사용에 enctype 나는이 스크립트의 도움을 바랍니다.

$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); 
0

만들기 u는 파일을 업로드하기 전에

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

다음 foloowing 코드를 작성하는이 기능

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

    } 
관련 문제