PHP裁剪在子文件夹中找到的图像以显示为缩略图

PHP裁剪在子文件夹中找到的图像以显示为缩略图,php,echo,thumbnails,Php,Echo,Thumbnails,我确实在某个地方找到了这个脚本,它似乎正在工作。唯一的问题是,它输出的拇指与原始拇指的大小比例完全相同,即使它们应该被裁剪成正方形 $dir = "*/"; $images = glob($dir."main.jpg" ); echo '<div class="projects-container">'; foreach( $images as $image ) { $dn = dirname($image); $thumbsDir = $dn; // path to the th

我确实在某个地方找到了这个脚本,它似乎正在工作。唯一的问题是,它输出的拇指与原始拇指的大小比例完全相同,即使它们应该被裁剪成正方形

$dir = "*/";
$images = glob($dir."main.jpg" );
echo '<div class="projects-container">'; 
foreach( $images as $image ) {
$dn = dirname($image);
$thumbsDir = $dn; // path to the thumbnails destination directory

    $imageName = "main.jpg"; // returns "cheeta.jpg"
    $thumbnail = $thumbsDir.$imageName; // thumbnail full path and name, i.e "./gallery/thumbs/cheeta.jpg"
    // for each image, get width and height
    $imageSize = getimagesize( $image ); // image size 
    $imageWidth = $imageSize[0];  // extract image width 
    $imageHeight = $imageSize[1]; // extract image height
    // set the thumb size
    if( $imageHeight > $imageWidth ){
        // images is portrait so set thumbnail width to 100px and calculate height keeping aspect ratio
        $thumbWidth = 200;
        $thumbHeight = floor( $imageHeight * ( 200 / imageWidth ) );           
        $thumbPosition  = "margin-top: -" . floor( ( $thumbHeight - 200 ) / 2 ) . "px; margin-left: 0";
    } else {
        // image is landscape so set thumbnail height to 100px and calculate width keeping aspect ratio
        $thumbHeight = 200;
        $thumbWidth = floor( $imageWidth * ( 200 / $imageHeight ) ); 
        $thumbPosition  = "margin-top: 0; margin-left: -" . floor( ( $thumbWidth - 200 ) / 2 ) . "px";
    } // END else if
    // verify if thumbnail exists, otherwise create it
    if ( !file_exists( $thumbnail ) ){
        $createFromjpeg = imagecreatefromjpeg( $image );
        $thumb_temp = imagecreatetruecolor( $thumbWidth, $thumbHeight );
        imagecopyresized( $thumb_temp, $createFromjpeg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight );
        imagejpeg( $thumb_temp, $thumbnail );
    } // END if()

echo '<div class="projects">';
echo '<div class="projects-img-container">';
echo" <a href='".$dn."'><img class='img-projet' src='". '/projects/' . $thumbnail . "'/></a>";
echo '</div>';
echo '</div>';
}
echo '</div>';
?> 
$dir=“*/”;
$images=glob($dir..main.jpg”);
回声';
foreach($images作为$image){
$dn=dirname($image);
$thumbsDir=$dn;//缩略图目标目录的路径
$imageName=“main.jpg”;//返回“cheeta.jpg”
$thumbnail=$thumbsDir.$imageName;//缩略图完整路径和名称,即“/gallery/thumbs/cheeta.jpg”
//对于每个图像,获取宽度和高度
$imageSize=getimagesize($image);//图像大小
$imageWidth=$imageSize[0];//提取图像宽度
$imageHeight=$imageSize[1];//提取图像高度
//设置拇指的大小
如果($imageHeight>$imageWidth){
//图像是纵向的,所以将缩略图宽度设置为100px,并计算高度保持纵横比
$thumbWidth=200;
$thumbHeight=地板($imageHeight*(200/imageWidth));
$thumbPosition=“页边距顶部:-”.floor($thumbHeight-200)/2.“px;页边距左侧:0”;
}否则{
//图像是横向的,所以将缩略图高度设置为100px,并计算宽度保持纵横比
$thumbHeight=200;
$thumbWidth=地板($imageWidth*(200/$imageHeight));
$thumbPosition=“页边距顶部:0;页边距左侧:-”.floor($thumbWidth-200)/2.“px”;
}//如果需要,则结束
//验证缩略图是否存在,否则创建缩略图
如果(!file_存在($thumbnail)){
$createFromjpeg=imagecreatefromjpeg($image);
$thumb_temp=ImageCreateTureColor($thumbWidth,$thumbHeight);
imagecopyresized($thumb_temp、$createFromjpeg、0、0、0、$thumbWidth、$thumbHeight、$imageWidth、$imageHeight);
图像JPEG($thumb\u temp,$thumbnail);
}//如果()结束
回声';
回声';
回声“;
回声';
回声';
}
回声';
?> 
你知道什么是错的吗


谢谢大家!

您完全没有错误处理,只是简单地假设不会出错,因此以下两行:

$dir = "*/";
$image2 = imagecreatefromjpeg($dir."main.jpg");
将完全等同于

$image2 = imagecreatefromjpeg("*/main.jpg");
icfj()不接受通配符,句号


由于您不检查错误,因此您永远不会看到icfj()返回的布尔值FALSE,表示失败。

很抱歉误解了这种情况。。。我忘了在IMG标记中添加一些CSS样式,这样图像的位置对应于$thumbPosition和项目IMG容器div,宽度和高度都为200px


现在一切正常

嗨,Marc,如果这个错误看起来很明显,很抱歉,但我对PHP完全陌生,我真的不知道如何进行错误检查。我能做些什么来避免这个通配符?我试着把$dn=dirname($image);在循环开始时,使用$dn而不是$dir,但id也不起作用。。。有没有关于如何做到这一点的提示?谢谢