2013-03-14 76 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