1

我最近第一次使用 PHP 建立了一个网站。我已经使用各种教程创建了一个画廊脚本,如果没有退出,它会自动拉出保存在设定位置的图像并创建一个缩略图。

现在我的上传工作正常,就像拉过图像一样,但是由于某种原因,脚本不会重新调整图像的大小!这真的开始让我感到困惑,因为我以前从未使用过 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

//只需在下面的函数中传递参数n你就可以得到调整大小的图像。

例如。

$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;

案例 'png' : $img = imagecreatefrompng ($sourcefile);

休息;

}

//$img = imagecreatefromjpeg($sourcefile);

$宽度 = 图像x( $img ); $高度=图像($img);

$比例= $拇指宽度/$宽度;

$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, $质量);

}

于 2013-03-14T13:16:06.243 回答