2014-07-18 4 views
-1

이미지 데이터베이스에서 가져 와서 동일한 이름과 이미지로 다른 폴더의 이미지를 복사하고 150x150으로 크기를 조정합니다.php를 사용하여 이미지 크기를 조정하는 방법은 무엇입니까?

나는 이미지 모으기에 어떤 문제가 있는지 알지 못합니다.

여기 내 PHP 코드입니다.

$s=mysql_query("select * from photo_gallery where image_id = '".$image_id."'"); 
$r = mysql_fetch_array($s); 

echo $filename = 'user_data/'.$r['image_name']; 
$filname1 = explode("/",$filename); 

echo $filname1[0]; echo "<br>"; 
echo $filename = $filname1[1]; 
+0

시도 [** Imagick :: resizeImage을 **] (http://php.net/manual/en/imagick.resizeimage.php) – adeneo

+1

고마워요.하지만 이해가 안됩니다. 나는 PHP에서 당신은 내게 정확한 코드를 줄 수있는 새로운 오전 –

+1

귀하의 질문에 확장하십시오. 우리에게 코드를 던지거나 모든 것이 당신을 위해 쓰여질 것이라고 기대하지 마십시오. – Darren

답변

0

나는 이것이 도움이된다고 생각합니다.

   $path = getcwd(); 
      $oldpic = $path.'/user_data/'.$r['image_name']; 
      $array = explode("/",$oldpic); 
      $count = count($array); 
      $name = $array[$count-1]; 

      $src = $oldpic; 
      $dest = $path."/user_data/thumbnail/".$name; 

      //Genrating the image from there extension 
      $size = getimagesize($src); 
      switch($size["mime"]){ 

         case "image/jpeg": 
          $source_image = imagecreatefromjpeg($src); //jpeg file 
         break; 

         case "image/gif": 
          $source_image = imagecreatefromgif($src); //gif file 
         break; 

         case "image/png": 
          $source_image = imagecreatefrompng($src); //png file 
         break; 

         default: 
          $source_image=false; 
         break; 
      } 
      $width = imagesx($source_image); 
      $height = imagesy($source_image); 
      $newwidth=150; 
      $newheight=150; 
      $virtual_image = imagecreatetruecolor($newwidth, $newheight); 
      imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
      imagejpeg($virtual_image,$dest,100); 

이미지는 'user_data/thumbnail'폴더에 저장됩니다.

+1

감사합니다. TBI 내 생명을 구했어. –

2
<?php 
function image_resize($imageName,$newName,$newWidth,$newHeight) 
{ 
     $imginfo = getimagesize($imageName); //get information about image 
     $type = $imginfo[2]; //third element of array is image type 
     if($type == IMAGETYPE_JPEG) { //if image is jpeg type 
     $image = imagecreatefromjpeg($imageName); 
     } elseif($type == IMAGETYPE_GIF) { // if image is gif type 
     $image = imagecreatefromgif($imageName); 
     } elseif($type == IMAGETYPE_PNG) { //if image is png type 
     $image = imagecreatefrompng($imageName); 
     } 
     $new_img = imagecreatetruecolor($newWidth, $newHeight); //create a new image 
     imagecopyresampled($new_img, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image)); 
     imagejpeg($new_img,$newName,75); //save the image as jpeg 
} 
//image_resize("original.jpg","output.jpg","150","150"); //how to use 
?> 

변화 아래 같은 PHP 코드 :이

$s=mysql_query("select * from photo_gallery where image_id = '".$image_id."'"); 
    $r = mysql_fetch_array($s); 

    $filename = 'user_data/'.$r['image_name']; 

    image_resize($filename,$filename."_thumbnail.jpg","150","150"); //how to use 

    echo 'orginial image is '.$filename.'<br> thumbnail is '.$filename."_thumbnail.jpg"; 
+0

당신은 또한 당신이 resize에서 x와 y 차원의 비율을 유지하기 위해 크기가 조정되는 이미지의 크기를 잡아야 할 것입니다 :'$ width = imagesx ($ image); $ x = imagesy ($ image);'(imagesx/y 함수의 입력은 원시 소스 이미지, 즉'imagecreatefrom *'의 결과입니다. 그래서'$ desired_width' 너비로 크기를 조정하려는 경우 , 다음'$ desired_height = floor ($ height * ($ desired_width/$ width)); ' – danyamachine

+0

감사합니다 Cattla –

관련 문제