2016-06-20 18 views
1

現在、クライアントのギャラリーページを作成しようとしています。クライアントがコードを変更する必要がないようにして、ギャラリーからすべての画像をフォルダからインポートして、同じサイズのサムネイルとして表示しようとしています。イメージのサイズが不明な場合、サムネイルをすべて同じサイズにする

すべての画像を正しく表示して表示でき、ライトボックスを開くことができました。問題はサムネイルの多くが正しく機能しないことです。アスペクト比を維持しながら余分な画像をカットしながら、画像を縮小するようにしています。画像がサムネイル用に設定したサイズよりも小さい場合は正しく動作するように見えますが、サイズが大きければ画像を正しい幅に縮小するように見えます。

enter image description here

私は本当にどこここから行くのか分からない:ここで私が何を意味するか表示する画像です。

Iは、好ましくはこれは私のメイク親指機能であるアスペクト比

を維持したいと思います:

function make_thumb($src,$dest,$desired_width, $desired_height, $ext) { 
    $size=480; 
    /* read the source image */ 
    if($ext == 'jpg' || $ext = 'jpeg') { 
      $source_image = imagecreatefromjpeg($src); 
    } 
    if($ext == 'png') { 
      $source_image = imagecreatefrompng($src); 
    } 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

    $ratio = $width/$height; 

    $targetWidth = $targetHeight = min($size, max($width, $height)); 

    if ($ratio < 1) { 
     $targetWidth = $targetHeight * $ratio; 
    } else { 
     $targetHeight = $targetWidth/$ratio; 
    } 

    $srcWidth = $width; 
    $srcHeight = $height; 
    $srcX = $srcY = 0; 

    $targetWidth = $targetHeight = min($width, $height, $size); 

    if ($ratio < 1) { 
     $srcX = 0; 
     $srcY = ($height/2) - ($width/2); 
     $srcWidth = $srcHeight = $width; 
    } else { 
     $srcY = 0; 
     $srcX = ($width/2) - ($height/2); 
     $srcWidth = $srcHeight = $height; 
    } 


    /* create a new, "virtual" image */ 
    $virtual_image = imagecreatetruecolor($targetWidth,$targetHeight); 
    /* copy source image at a resized size */ 
    imagecopyresized($virtual_image,$source_image,0,0,$srcX,$srcY,$targetWidth,$targetHeight,$srcWidth,$srcHeight); 
    /* create the physical thumbnail image to its destination */ 
    if($ext == 'jpg' || $ext = 'jpeg') { 
      imagejpeg($virtual_image,$dest); 
    } 
    if($ext == 'png') { 
      imagepng($virtual_image,$dest); 
    } 
} 
+1

にあなたが 'add_image_sizeを使用しない理由:

まず、私は2番目のは、私が変更しなければならなかった

の$ EXT = 'JPEG' の代わりに==を持っていました名前、幅、高さ、真) ' – deemi

+0

私はadd_images_size()がwordpressのみであるという印象を受けましたか? –

答えて

0

私は自分の問題を考え出しました。 「(

if ($ratio < 1) { 
    $targetWidth = $targetHeight * $ratio; 
} else { 
    $targetHeight = $targetWidth/$ratio; 
} 

if ($ratio < 1) { 
    $targetWidth = $targetHeight/$ratio; 
} else { 
    $targetHeight = $targetWidth/$ratio; 
} 
関連する問題