在PHP中创建缩略图时出错

时间:2013-11-25 10:28:34

标签: php thumbnails

这是我创建缩略图的代码。在这里,我试图检查缩略图是否已经存在。如果是,它会直接显示。否则创建缩略图然后显示。

<a class="adiimage" rel="colorbox" title=" " href="new/artists/data/single_image.php?imageid=<?php echo $rowa_image['product_image_id']; ?>">
            <!--<img src="new/images/upload/<?php echo $image; ?>"  alt="" border="0" />-->
        <?php
                $addr = $_SERVER['HOME'];
                echo '<script> alert("'.$addr.'"); </script>';
                /*$pathToImages = "../new/images/upload/";
                $pathToThumbs = "../new/images/upload/thumbs/";*/
                $pathToImages = "../new/images/upload/";
                $pathToThumbs = "../new/images/upload/thumbs/";
                $thumbWidth = 100;
                $filename = $pathToThumbs . $image;
                //check if thumbnail exists
                if(file_exists($filename)){ 
                    //display thumbnail
                ?>
                    <img src="<?php echo $filename; ?>"  alt="" border="0" />
                <?php
                }
                else{
                    //create thumbnail
                    createThumbs($pathToImages, $pathToThumbs, $thumbWidth, $image);
                    //createThumbs("new/images/upload/","new/images/upload/thumbs/",100);
                ?>
                    <img src="<?php echo $filename; ?>"  alt="" border="0" />
                <?php
                }
            } 
        ?>
        </a>
         <!--</div>-->
    </li>
<?php  }   }   } ?>
</ul>
</div>


<?php 

//thumbnail creation 
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth, $image ) 
{
// open the directory
//$dir = opendir( $pathToImages );
$dir = fopen($pathToImages, "rw");
/* read the source image */
$file = $pathToImages . $image;
//echo '<script> alert("'.$file.'"); </script>';
$source_image = imagecreatefromjpeg($pathToImages);
//$source_image = imagecreatefromjpeg( "{$pathToImages}{$image}" );

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

/* find the "desired height" of this thumbnail, relative to the desired width  */
$desired_height = floor($height * ($thumbWidth / $width));

/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($thumbWidth, $desired_height);

/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $thumbWidth, $desired_height, $width, $height);

/* create the physical thumbnail image to its destination */
//imagejpeg($virtual_image, $pathToThumbs);
imagejpeg($virtual_image, "{$pathToThumbs}{$image}");
}
?>

上面的代码给了我以下错误,我不知道如何解决。我认为文件路径是问题。但我不确定究竟是什么错误。

Warning: fopen(../new/images/upload/) [
function.fopen
]: failed to open stream: No such file or directory in  /home/artistiv/public_html/new/publicprofile/artist_image.php on line 107

Warning: imagecreatefromjpeg(../new/images/upload/) [
function.imagecreatefromjpeg
]: failed to open stream: No such file or directory in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 111

Warning: imagesx() expects parameter 1 to be resource, boolean given in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 114

Warning: imagesy() expects parameter 1 to be resource, boolean given in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 115

Warning: Division by zero in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 118

Warning: imagecreatetruecolor() [
function.imagecreatetruecolor
]: Invalid image dimensions in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 121

Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 124

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in     /home/artistiv/public_html/new/publicprofile/artist_image.php on line 128

当我使用opendir函数时,它给了我以下错误:

function.imagecreatefromjpeg
]: failed to open stream: No such file or directory in /home/artistiv/public_html/new/publicprofile/artist_image.php on line 110

2 个答案:

答案 0 :(得分:1)

错误不言自明:

  • 您在/home/artistiv/public_html/new/publicprofile/artist_image.php
  • 您要求打开等于../new/images/upload/
  • /home/artistiv/public_html/new/new/images/upload/
  • 没有这样的文件或目录

您还使用rw标记 - 我在list of possible modes中找不到它们:

  • r 仅供阅读
  • w 仅供写作

PHP如何同时做两件事?

答案 1 :(得分:0)

您的文件路径似乎是/ home / artistiv / public_html / new / publicprofile /

你的Path变量应该像这样设置:

$ pathToImages =“../ images / upload /”; $ pathToThumbs =“../ images / upload / thumbs /”;

$ pathToImages =“../../new/images/upload/”; $ pathToThumbs =“../../ new / images / upload / thumbs /”;