2016-11-06 8 views
0

私はそれが必要な場所と表示したいサイズに従って画像のサイズを変更するこのphp関数を持っています。 しかし、私の問題は、イメージにPHPのコンテンツタイプを設定せずに、HTMLイメージ要素のサイズ変更されたイメージを表示するように設定する方法ですか? コンテンツタイプをイメージに設定すると、イメージがページ全体に表示されます。削除すると、未知の長い文字が出力されます。これを行うにはどうしたらいいですか?Php画像のサイズを変更して出力を

<?php 
    function CroppedThumbnail($imagename, $imgwidth, $imgheight){ 
// Set a maximum height and width 
$width = $imgwidth; 
$height = $imgheight; 
// Content type 
//header('Content-Type: image/jpeg'); 
// Get new dimensions 
list($width_orig, $height_orig) = getimagesize($imagename); 
$ratio_orig = $width_orig/$height_orig; 
if ($width/$height > $ratio_orig) { 
    $width = $height*$ratio_orig; 
} else { 
    $height = $width/$ratio_orig; 
} 
// Resample 
$image_p = imagecreatetruecolor($width, $height); 
$image = imagecreatefromjpeg($imagename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 
// Output 
imagejpeg($image_p, $imagename, 100); 
return $imagename; 
} 

$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg'; 
$filename2 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Small.jpg'; 
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>'; 
echo '<img data="image2" src="'.CroppedThumbnail($filename2, 100, 100).'"/>'; 
    ?> 

は、私は私があなたのコード編集した

+0

imagejpegは、真または偽のブール値を返します。 2番目のパラメータをファイル名として使用し、ファイル名とパスを返す必要があります。 – KiwiJuicer

+0

@KiwiJuicer返信するファイル名のパスがわかった –

+0

あなたはそうしたのですか?さて、実際に作成されたファイルは、imgのソースで使用する必要があります。あなたの場合、現在、2番目のパラメタnullのために、イメージストリーム全体が取得されます。 – KiwiJuicer

答えて

1
$newFile = 'images/newFile.jpg'; 

imagejpeg($image_p, $newFile, 100); 

return '/' . $newFile; 

でこの関数を呼び出す任意の画像のサイズを変更したい:

<?php 
function CroppedThumbnail($imagename, $imgwidth, $imgheight){ 
// Set a maximum height and width 
    $width = $imgwidth; 
    $height = $imgheight; 
// Content type 
//header('Content-Type: image/jpeg'); 
// Get new dimensions 
    list($width_orig, $height_orig) = getimagesize($imagename); 
    $ratio_orig = $width_orig/$height_orig; 
    if ($width/$height > $ratio_orig) { 
     $width = $height*$ratio_orig; 
    } else { 
     $height = $width/$ratio_orig; 
    } 
// Resample 
    $image_p = imagecreatetruecolor($width, $height); 
    $image = imagecreatefromjpeg($imagename); 
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 
// Output 
    $newFileName = 'images/newFile.jpg'; 
    imagejpeg($image_p, $newFileName, 100); 

    return '/' . $newFileName; 
} 
$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg'; 
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>'; 
?> 
+0

がこれをしましたが、出力イメージが大きくなりました –

+0

あなたのエラーは何ですか?フォルダイメージが存在する必要があります! – KiwiJuicer

+0

申し訳ありませんがコードとコンテンツの種類と出力イメージの前に他の出力を削除しました –

0

私はあなたのコードに簡単な変更を示唆することになるだろうあなたの要件を満たすだけでなく、再利用可能なものになります。

ここでは、サイズ変更機能を別々のファイルに分割することができます。

resize.php

<?php 
function CroppedThumbnail($imagename, $imgwidth, $imgheight){ 

// Set a maximum height and width 
    $width = $imgwidth; 
    $height = $imgheight; 

// Get new dimensions 
    list($width_orig, $height_orig) = getimagesize($imagename); 
    $ratio_orig = $width_orig/$height_orig; 

    if ($width/$height > $ratio_orig) { 
    $width = $height*$ratio_orig; 
    } else { 
    $height = $width/$ratio_orig; 
    } 

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

    // Output 
    return imagejpeg($image_p, null, 100); 
} 

    header('Content-Type: image/jpeg'); 
    echo CroppedThumbnail($_GET['imagename'], $_GET['width'], $_GET['height']) 
?> 

今、私たちは要求にすることにより、画像の名前、高さと幅を渡してリサイズした画像をレンダリングするために、ファイルのサイズを変更する上で使用することができます。
example.php

<?php 
    $filename1 = 'path and file name of image 1 here'; 
    $filename2 = 'path and file name of image 2 here'; 

    echo '<img data="image1" src="imagetest.php?imagename='. $filename1 .'&height=100&width=100"/>'; 
    echo '<img data="image2" src="imagetest.php?imagename='. $filename2 .'&height=100&width=100"/>'; 
?> 
関連する問題