我确实在某个地方找到了这个脚本,它似乎正在工作。唯一的问题是它输出的拇指尺寸与原始尺寸完全相同,即使它们被裁剪成方形......
$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>';
?>
任何可能出错的想法?
谢谢!
答案 0 :(得分:3)
你绝对没有错误处理,只是假设什么都不会出错,所以这两行:
$dir = "*/";
$image2 = imagecreatefromjpeg($dir."main.jpg");
将完全等同于
$image2 = imagecreatefromjpeg("*/main.jpg");
icfj()执行 NOT 接受通配符,句点。
由于你没有检查错误,你永远不会看到icfj()返回的布尔值为FALSE,表示失败。
答案 1 :(得分:1)
对于对这种情况的误解感到抱歉...我忘了在IMG标签中添加一些CSS样式,以便图像的位置对应于$ thumbPosition,并且对应于items-img-container div有宽度和200px的高度。
现在一切正常!