2016-11-11 55 views
5

thực sự, tôi muốn điều đó, khi tôi tải lên 1 hình ảnh thì hình ảnh gốc sẽ được lưu trữ trong upload/og/. Nhưng tôi cũng muốn hình ảnh đó ở các kích thước khác nhau, chẳng hạn như 1366x768, 1280*600, 768x1024 vv ... không chỉ các kích thước này, nó sẽ tỷ lệ thuận với Hình ảnh đó.Cách tạo hình ảnh đơn trong các kích thước/kích thước khác nhau

Tôi có một mã chuyển đổi hình ảnh đó thành ngón tay cái theo tỷ lệ, điều này hoạt động max-width=300max-height=600.

define ("MAX_SIZE","100"); 
define ("WIDTH","300"); 
define ("HEIGHT","600"); 
function make_thumb($img_name,$filename,$new_w,$new_h) 
{ 
    $ext=getExtension($img_name); 
    if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) 
     $src_img=imagecreatefromjpeg($img_name); 
    if(!strcmp("png",$ext)) 
     $src_img=imagecreatefrompng($img_name); 

    //gets the dimmensions of the image 
    $old_x=imageSX($src_img); 
    $old_y=imageSY($src_img); 

    // next we will calculate the new dimmensions for the thumbnail image 
    $ratio1=$old_x/$new_w; 
    $ratio2=$old_y/$new_h; 
    if($ratio1>$ratio2) { 
     $thumb_w=$new_w; 
     $thumb_h=$old_y/$ratio1; 
    } 
    else { 
     $thumb_h=$new_h; 
     $thumb_w=$old_x/$ratio2; 
    } 
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); 
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // resize the big image to the new created one 

    if(!strcmp("png",$ext)) // output the created image to the file. Now we will have the thumbnail into the file named by $filename 
     imagepng($dst_img,$filename); 
    else 
     imagejpeg($dst_img,$filename); 

    imagedestroy($dst_img); 
    imagedestroy($src_img); 
} 
function getExtension($str) { 
    $i = strrpos($str,"."); 
    if (!$i) { return ""; } 
    $l = strlen($str) - $i; 
    $ext = substr($str,$i+1,$l); 
    return $ext; 
} 

$errors=0; 
if(isset($_POST['submit'])) 
    { 
    //reads the name of the file the user submitted for uploading 
    $image=$_FILES['scrnsots']['name']; 
    if ($image) 
    { 
     $filename = stripslashes($_FILES['scrnsots']['name']); 
     // get the extension of the file in a lower case format 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
     // if it is not a known extension, we will suppose it is an error, print an error message 
     //and will not upload the file, otherwise we continue 
     if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) 
     { 
      echo '<h1>Unknown extension!</h1>'; 
      $errors=1; 
     } 
     else 
     { 
      $size=getimagesize($_FILES['scrnsots']['tmp_name']); 
      $sizekb=filesize($_FILES['scrnsots']['tmp_name']); 
      if ($sizekb > MAX_SIZE*102400) 
      { 
       echo '<h1>You have exceeded the size limit!</h1>'; 
       $errors=1; 
      } 
      $image_name= $image .''. time() .'.'.$extension; 
      $newname="uploads/scrnsots/".$image_name; 
      $copied = copy($_FILES['scrnsots']['tmp_name'], $newname); 
      if (!$copied) 
      { 
       echo '<h1>Copy unsuccessfull!</h1>'; 
       $errors=1; 
      } 
      else 
      { 
       $thumb_name='uploads/scrnsots/thumb/thumb-'.$image_name; 
       $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); 
      } 
     } 
    } 
} 
if(isset($_POST['submit']) && !$errors) 
{ 
    echo $thumb_name ."<br/>"; 


     echo "<div class='custom alert alert-success'>Successfully Added.<a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a></div>"; 
} 

nhưng nó sẽ chỉ tạo ngón tay cái. tôi muốn 1 hình ảnh khác nhau - dimentions (chiều cao - chiều rộng).

+0

Bạn đã thử đặt hàm này trong một vòng lặp với các kích thước thích hợp làm tham số và nó phải làm như bạn yêu cầu. – Rasclatt

+0

xin lỗi, bro. tôi không có đủ kiến ​​thức về php. nhưng tôi muốn tạo trang web như http://www.hdwallpapers.in/2017_wonder_woman_movie-wallpapers.html –

Trả lời

1

sau rất nhiều cố gắng, tôi tìm thấy câu trả lời. bằng cách thay đổi một số điều dưới đây dòng

else 
    { 
     $thumb_name='uploads/scrnsots/thumb/thumb-'.$image_name; 
     $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); 
    } 

để

else 
    { 
     $cars = array("300", "800", "1280","1366","1920"); 
     $arrlength = count($cars); 
     for($x = 0; $x < $arrlength; $x++) { 
     echo $cars[$x]; 
     $thumb_name='uploads/scrnsots/'.$cars[$x].'/thumb-'.$image_name_dash; 
     $thumb=make_thumb($newname,$thumb_name,$cars[$x],$cars[$x]); 
      list($width_main, $height_main, $type, $attr) = getimagesize($newname); 
      list($og_image_w, $og_image_h, $type, $attr) = getimagesize("$thumb_name"); 
      echo "<br>"; 
     } 
    } 

và nó hoạt động. hoàn hảo. ý tưởng này xuất phát từ http://www.w3schools.com/php/php_arrays.asp tôi chỉ tạo 5 thư mục vào đường dẫn đích có tên là "300", "800", "1280","1366","1920".

Sau đó, nó tạo ra những hình ảnh cụ thể trong thư mục cụ thể một cách hoàn hảo. nghiêm túc, tôi rất hạnh phúc.

2

Như đã nhận xét, bạn thực sự chỉ phải đặt phần cuối cùng (hàm) trong vòng lặp và thêm một số logic tạo thư mục bằng cách sử dụng mkdir(). Cũng có một chức năng có nguồn gốc trích xuất các phần mở rộng được gọi là pathinfo(), vì vậy chỉ cần sử dụng một thay vì một trong những bạn có:

function make_thumb($img_name,$filename,$new_w,$new_h) 
    { 
     $ext=getExtension($img_name); 
     if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) 
      $src_img=imagecreatefromjpeg($img_name); 
     if(!strcmp("png",$ext)) 
      $src_img=imagecreatefrompng($img_name); 

     $old_x=imageSX($src_img); 
     $old_y=imageSY($src_img); 

     $ratio1=$old_x/$new_w; 
     $ratio2=$old_y/$new_h; 
     if($ratio1>$ratio2) { 
      $thumb_w=$new_w; 
      $thumb_h=$old_y/$ratio1; 
     } 
     else { 
      $thumb_h=$new_h; 
      $thumb_w=$old_x/$ratio2; 
     } 
     $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h); 
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // resize the big image to the new created one 

     if(!strcmp("png",$ext)) // output the created image to the file. Now we will have the thumbnail into the file named by $filename 
      imagepng($dst_img,$filename); 
     else 
      imagejpeg($dst_img,$filename); 

     imagedestroy($dst_img); 
     imagedestroy($src_img); 
    } 

if(!empty($_FILES)) { 
    $errors=0; 
    if($_POST['submit']) { 
     if($_FILES['scrnsots']['error'] == 0) { 
      $image = $_FILES['scrnsots']['name']; 
      $filename = stripslashes($_FILES['scrnsots']['name']); 
      $extension = pathinfo($filename,PATHINFO_EXTENSION); 
      $extension = strtolower($extension); 
      if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) { 
       echo '<h1>Unknown extension!</h1>'; 
       $errors=1; 
      } 
      else { 
       $size = getimagesize($_FILES['scrnsots']['tmp_name']); 
       $sizekb = filesize($_FILES['scrnsots']['tmp_name']); 
       if ($sizekb > MAX_SIZE*102400) { 
        echo '<h1>You have exceeded the size limit!</h1>'; 
        $errors=1; 
       } 

       $image_name = $image.''.time().'.'.$extension; 
       $newname = __DIR__."/uploads/scrnsots/".$image_name; 
       $basepath = pathinfo($newname,PATHINFO_DIRNAME); 
       # Create the folder if not already made 
       if(!is_dir($basepath)) 
        mkdir($basepath,0755,true); 
       # It's more common to use this function here instead of copy() 
       if (!move_uploaded_file($_FILES['scrnsots']['tmp_name'], $newname)) { 
        echo '<h1>Copy unsuccessfull!</h1>'; 
        $errors=1; 
       } 
       else { 
        # Have your sizes here 
        $sizes = array(
         array(1366,768), 
         array(1280,600), 
         array(768,1024) 
        ); 
        # Loop through your sizes here and apply your function 
        foreach($sizes as $hw) { 
         $thumb_name = __DIR__.'/uploads/scrnsots/thumb/'.$hw[0].$hw[1].'/thumb-'.$image_name; 
         $thumb_path = pathinfo($thumb_name,PATHINFO_DIRNAME); 
         # Create the thumbnail directory(s) if not exist 
         if(!is_dir($thumb_path)) 
          mkdir($thumb_path,0755,true); 
         # Run your function 
         make_thumb($newname,$thumb_name,$hw[0],$hw[1]); 
        } 
       } 
      } 
     } 
    } 
} 
Các vấn đề liên quan