1

最近、初めて PHP を使用して Web サイトを作成しました。さまざまなチュートリアルを使用して、設定された場所に保持されている画像を自動的に取得し、サムネイルが存在しない場合はサムネイルを作成するギャラリー スクリプトを作成しました。

これで、画像をプルするのと同じようにアップロードが正常に機能しますが、何らかの理由でスクリプトが画像のサイズを変更しません! これは本当に私を困惑させ始めています.phpを使用したことがないので、完全に途方に暮れています. 以下の私のスクリプト

<?php
    # SETTINGS
    $max_width = 100;
    $max_height = 100;

    function getPictureType($ext) {
            if ( preg_match('/jpg|jpeg/i', $ext) ) {
                    return 'jpg';
            } else if ( preg_match('/png/i', $ext) ) {
                    return 'png';
            } else if ( preg_match('/gif/i', $ext) ) {
                    return 'gif';
            } else {
                    return '';
            }
    }

    function getPictures() {
            global $max_width, $max_height;
            if ( $handle = opendir("Design/Images/Gallery/") ) {
                    $lightbox = rand();
                    echo '<div id="gallery"><ul>';
                    while ( ($file = readdir($handle)) !== false ) {
                            if ( !is_dir($file) ) {
                                    $split = explode('.', $file); 
                                    $ext = $split[count($split) - 1];
                                    if ( ($type = getPictureType($ext)) == '' ) {
                                            continue;
                                    }
                                    if ( ! is_dir('Design/Images/Gallery/thumbs') ) {
                                            mkdir('Design/Images/Gallery/thumbs');
                                    }
                                    if ( ! file_exists('Design/Images/Gallery/thumbs/'.$file) ) {
                                            if ( $type == 'jpg' ) {
                                                    $src = imagecreatefromjpeg($file);
                                            } else if ( $type == 'png' ) {
                                                    $src = imagecreatefrompng($file);
                                            } else if ( $type == 'gif' ) {
                                                    $src = imagecreatefromgif($file);
                                            }
                                            if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
                                                    $newW = $oldW * ($max_width / $oldH);
                                                    $newH = $max_height;
                                            } else {
                                                    $newW = $max_width;
                                                    $newH = $oldH * ($max_height / $oldW);
                                            }
                                            $new = imagecreatetruecolor($newW, $newH);
                                            imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
                                            if ( $type == 'jpg' ) {
                                                    imagejpeg($new, 'Design/Images/Gallery/thumbs/'.$file);
                                            } else if ( $type == 'png' ) {
                                                    imagepng($new, 'Design/Images/Gallery/thumbs/'.$file);
                                            } else if ( $type == 'gif' ) {
                                                    imagegif($new, 'Design/Images/Gallery/thumbs/'.$file);
                                            }
                                            imagedestroy($new);
                                            imagedestroy($src);
                                    }
                                    echo '<li><a href="Design/Images/Gallery/'.$file.'" rel="lightbox['.$lightbox.']">';
                                    echo '<img src="Design/Images/Gallery/thumbs/'.$file.'" alt="" />';
                                    echo '</a></li>';
                            }
                    }
                    echo '</ul></div>';
            }
    }

?>

エラーは私のパスに関係していると思いますが、よくわかりません。誰かがこれに光を当てることができますか????

4

1 に答える 1

0

//以下の関数にパラメーターを渡すだけで、サイズ変更された画像が取得されます。

例のために。

$sourcefile = SITE_PATH."pro_images/".$main_img_name;

$thumb = "thumb_".$main_img_name;   

$endfile =  SITE_PATH."pro_images/thumb/".$thumb;

    $thumbheight = 80;

    $thumbwidth = 80;

    $quality = 75;




function createThumbs($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality){

preg_match("'^(.*).(gif|jpe?g|png)$'i", $sourcefile, $ext);

スイッチ (strtolower($ext[2])) {

   case 'jpg' : 

   case 'jpeg': $img  = imagecreatefromjpeg ($sourcefile);

                 break;
   case 'gif' : $img  = imagecreatefromgif  ($sourcefile);

                 break;

case 'png' : $img = imagecreatefrompng ($sourcefile);

壊す;

}

//$img = imagecreatefromjpeg($sourcefile);

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

$scale = $thumbwidth/$width;

$newwidth = ceil($width * $scale);
$newheight = ceil($height * $scale);

// 新しい一時イメージを作成します。

$tmpimg = imagecreatetruecolor( $newwidth, $newheight );

// 古い画像を新しい画像にコピーしてサイズ変更します。

imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );

// サムネイルをファイルに保存します。

imagejpeg( $tmpimg, $endfile, $quality);

}

于 2013-03-14T13:16:06.243 に答える