2013-03-14 116 views
1

我最近用PHP首次構建了一個網站。我已經創建了一個使用各種教程的畫廊腳本,它可以自動拉取設置位置中的圖像,並創建一個縮略圖(如果不退出)。PHP Image Resize(gallery)

現在我上傳的正常工作一樣通過圖像拉動,但是腳本出於某種原因,不會重新大小的圖像!這真的開始困擾我了,因爲我以前從未使用過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>'; 
      } 
    } 

?>

我認爲錯誤的東西與我的路徑,但我不知道,任何人都可以提供一些線索這光????

回答

0

//只需在下面的函數中傳遞參數即可獲得調整大小的圖像。例如:

$的資源文件= 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)$' 我」,$的資源文件,$ EXT);

開關(用strtolower($ EXT [2])){

case 'jpg' : 

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

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

       break; 

案 'PNG':$ IMG = imagecreatefrompng($的資源文件);

break;

}

// $ IMG = imagecreatefromjpeg($的資源文件);

$ 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,$寬度,$高度);

//將縮略圖保存到文件中。

imagejpeg($ tmpimg,$ ENDFILE,$質量);

}

+0

對不起,是愚蠢的,但我真的這個迷惑(我是一個總的新手!)。我在自己的代碼中錯過了什麼嗎? – Callum 2013-03-14 13:18:37

+1

如果問題是關於路徑比你應該給圖像文件夾的絕對路徑可能這會幫助你。 – ajay 2013-03-14 13:23:31

+1

像絕對路徑 - D:/xampplite/htdocs/image/image.jpg – ajay 2013-03-14 13:25:20