0

私はphpが初めてです。画像をアップロードしてサイズを変更し、保存せずに表示しようとしています。私はこれを行うために gd を使用しています。ここで提供されているコードは、関数が機能するための基本的なアプローチにすぎません。

<?php
if (!isset($_FILES['image']['tmp_name'])) {
}   
else{
$file=$_FILES['image']['tmp_name'];
$img_src = imagecreatefromstring(file_get_contents($file));
$img_dest = imagecreatetruecolor(851, 315);
$src_width = imagesx($img_src);
$src_height = imagesy($img_src);
imagecopyresized($img_dest, $img_src, 0, 0, 0, 0, 851, 315, $src_width, $src_height);
$text= $_POST['text'];
$font_path = 'arial.TTF';
$grey = imagecolorallocate($img_dest, 128, 128, 128);
$black = imagecolorallocate($img_dest, 0, 0, 0);
imagettftext($img_dest, 25, 0, 302, 62, $grey, $font_path, $text);
imagettftext($img_dest, 25, 0, 300, 60, $black, $font_path, $text);
header( "Content-type: image/png" );
imagepng( $img_dest );
imagedestroy( $img_dest );
imagedestroy( $img_src );
}
?>

フォームから画像をアップロードし、このスクリプトを実行しています。画像が表示されています。しかし、この方法を使用して複数の画像を異なるサイズで表示するにはどうすればよいですか。よろしく。

4

2 に答える 2

0

2 つのイメージを作成する必要があります。ソースから直接作成できるもの

$img_src = imagecreatefrompng($file);

また

$img_src = imagecreatefromjpeg($file);

また

$img_src = imagecreatefromstring(file_get_contents($file));

src ファイルのファイル サイズを取得します。

$sizes = imagesize($img_src);
$src_width = $sizes[0];
$src_height = $sizes[1];

ただし、src 画像の高さが同じでない場合でも、画像は 200x200 にスケーリングされます。これは、dst-size を計算することで防ぐことができます。

$faktor = ($src_width > $src_height ? $src_width : $src_height);
$faktor = 100 / $faktor;

$f_width = round($src_width * $faktor);
$f_height = round($src_height * $faktor);

$new_w = 200 * $f_width;
$new_h = 200 * $f_height;

目的のサイズで作成できる2番目のもの

$img_dest = imagecreatetruecolor($new_w, $new_h);

そして、サイズ変更されたソースを新しいソースにコピーできます

imagecopyresized($img_dest, $img_src, 0, 0, 0, 0, $new_w, $new_h, $src_width, $src_height);
header( "Content-type: image/png" );
imagepng( $img_dest );
imagedestroy( $img_dest );
imagedestroy( $img_src );

PS: 文字列から画像を作成するとき、コンテンツにスラッシュを追加するのは良くないと思います。

于 2013-07-12T07:53:42.127 に答える