2011-06-22 19 views

答えて

0

あなたはimagecopyresampled PHPの関数を使用することができます。新しいサイズを計算することもできます。

8

私はこの質問は、実際のコード例で解答を使用することができると思います。下のコードは、ディレクトリuploaded内のイメージのサイズを変更し、サイズ変更されたイメージをフォルダresizedに保存する方法を示しています。

<?php 
// the file 
$filename = 'uploaded/my_image.jpg'; 

// the desired width of the image 
$width = 180; 

// content type 
header('Content-Type: image/jpeg'); 

list($width_orig, $height_orig) = getimagesize($filename); 

$ratio_orig = $width_orig/$height_orig; 
$height = $width/$ratio_orig; 

// resample 
$image_p = imagecreatetruecolor($width, $height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 

// output 
imagejpeg($image_p, 'resized/my_image.jpg', 80); 
?> 
4

まずあなたは、現在の画像のサイズを取得する必要があります:ちょうど画像の新しい高さを計算し、スケーリング係数を持つとき

$scalingFactor = $newImageWidth/$width; 

$width = imagesx($image); 
$height = imagesy($image); 

が続いてスケーリング係数を計算します:

$newImageHeight = $height * $scalingFactor; 

次に、新しいイメージを作成します。

$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); 
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height); 

おそらく、これらのスニペットは役立ちます:

http://www.codeslices.net/snippets/resize-scale-image-proportionally-to-given-width-in-phphttp://www.codeslices.net/snippets/resize-scale-image-proportionally-in-php

少なくとも彼らは私のために働きました。

関連する問題