简体   繁体   English

显示来自TABLE的具有不同尺寸的图像

[英]Display Images from TABLE with different sizes of individual

I have this plan on my design to fetch all the images from table and to resize the images in any sizes individually. 我的设计计划是从表中获取所有图像,并分别调整任何尺寸的图像的大小。 So the classnames are the key to change the sizes. 因此,类名是更改大小的关键。 How can I display it with different sizes? 如何显示不同的尺寸?

include 'conn.php';

$viewquery= "SELECT id, product_image, product_cat FROM products WHERE product_cat LIKE 'Sale'";

$result = mysqli_query($connection, $viewquery);
$fetch = mysqli_fetch_assoc($result);

echo' <div class="div-frame-five">';
    echo' <img src="images/'.$fetch["product_image"].'" alt="" class="image-one-frame">';
    echo' <img src="images/'.$fetch["product_image"].'" alt="" class="image-two-frame">';
    echo' <img src="images/'.$fetch["product_image"].'" alt="" class="image-three-frame">';
    echo' <img src="images/'.$fetch["product_image"].'" alt="" class="image-four-frame">';
echo' </div>';

Example

示例图片

You can format you images using css, using your classes. 您可以使用类,使用CSS格式化图像。 For example - 例如 -

img {
  height: 200px; // set a standard height
  padding-bottom: 5px; // add a space between the img rows
}
.image-one-frame, .image-four-frame {
  width: 66%; // make one and four 2/3 the width
}
.image-two-frame, .image-three-frame {
  width: 33%; // make two and three 1/3 the width
}
.image-one-frame, .image-three-frame {
  float: left; // make one and three float left
}
 .image-two-frame, .image-four-frame {
  float: right; // make two and four float right
}

see https://jsfiddle.net/5ry7dLug/10/ 参见https://jsfiddle.net/5ry7dLug/10/

This will iterate through you result set and and alternate your img width between 66% and 33%. 这将遍历您的结果集,并在66%到33%之间更改img宽度。 There are many other ways to go about this but this will probably be the simplest approach for now. 还有许多其他方法可以解决此问题,但这可能是目前最简单的方法。

Try this: 尝试这个:

include 'conn.php';

$viewquery= "SELECT id, product_image, product_cat FROM products WHERE product_cat LIKE 'Sale' AND id LIKE '23'";

$result = mysqli_query($connection,$viewquery);
$fetch = mysqli_fetch_all($result, MYSQLI_ASSOC)

echo '<div class="div-frame-five">';

$size = TRUE;

foreach($fetch as $image) {
    echo '<img src="images/' . $image["product_image"] . '" alt="" class="image-one-frame" style="width:' . (($size = !$size) ? '66%' : '33%') . ';">';
}

echo '</div>';

From what I gather from the comments, you are looking for both how to iterate and display the images in the example pattern. 根据我从评论中收集的信息,您正在寻找如何迭代和显示示例模式中的图像。 If you are looking to set the dimensions of the images from the database, you would need to change your table to include the image dimensions. 如果要设置数据库中图像的尺寸,则需要更改表格以包括图像尺寸。

First in order to retrieve and iterate over each of the images in the database. 首先是为了检索和遍历数据库中的每个图像。 You can use the following. 您可以使用以下内容。

Example: https://3v4l.org/5WOEJ 例如: https//3v4l.org/5WOEJ

<?php
require_once __DIR__ . '/conn.php';

$viewquery= 'SELECT id, product_image, product_cat 
FROM products 
WHERE product_cat LIKE "Sale"';

if ($result = mysqli_query($connection, $viewquery)) {
    $images = mysqli_fetch_all($result, MYSQLI_NUM);
?>
    <div class="div-frame-five">
        <?php foreach ($images as list($id, $src, $cat)) { ?>
        <img src="/images/<?php echo $src; ?>" alt=""/>
        <?php } ?>
    </div> 
<?php 
    mysqli_free_result($result);
} 
?>

You can also sort the results by the size of the images, using getimagesize() , if the saved images are already sized and the dimensions are not stored in the database. 如果保存的图像已经调整大小并且尺寸未存储在数据库中,则还可以使用getimagesize()按图像的大小对结果进行排序。

Notes: 笔记:

  • You should always use require for scripts that can not function without the included resource 您应该始终将require用于没有随附资源就无法运行的脚本
  • You can use require_once to prevent multiple instances of the database connection from being created 您可以使用require_once防止创建数据库连接的多个实例
  • It is best-practice to specify the full path to the include file to avoid the include_path from searching for it and reduce ambiguity 最佳做法是指定包含文件的完整路径,以避免include_path搜索该文件并减少歧义
  • it is recommended to validate your query results are not false before using fetch 建议在使用fetch之前验证您的查询结果是否为false
  • free_result is a best-practice to reduce the memory used on larger scripts/datasets free_result是减少大型脚本/数据集上使用的内存的最佳实践
  • using fetch_all retrieves the entire dataset into a single array, which uses more memory but is faster 使用fetch_all将整个数据集检索到一个数组中,该数组使用更多的内存但速度更快

If you are wanting to resize the physical image dimensions, you can use the GD extension to achieve a resized image. 如果要调整物理图像尺寸,可以使用GD扩展名来调整图像尺寸。 The below example uses imagecopyresampled and only works with JPG images. 下面的示例使用imagecopyresampled ,仅适用于JPG图像。 When a height is specified, if the source image does not fit the resized aspect ratio, the background of the cropped portions will be black. 当指定高度时,如果源图像不适合调整后的宽高比,则裁切部分的背景将为黑色。

 <?php 
 function jpg_thumbnail($src, $dest, $new_width, $new_height)
 {
    if (is_file($dest)) {
        //specified file already exists
        return;
        /* unlink($dest); //alternatively delete the file instead */
    }
    /* load the source image and retrieve its dimensions */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* create a blank image with the desired dimensions */
    $new_image = imagecreatetruecolor($new_width, $new_height);

    /* copy the source image onto the blank image, using the desired dimensions */
    imagecopyresampled($new_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    /* save the new image as a new file */
    imagejpeg($new_image, $dest);
 }

 $i = 0;
 foreach ($images as list($id, $src, $cat)) {
     $desired_width = 132; //33% of 400px
     if (in_array(++$i, [1, 4], false)) {
        $desired_width = 264; //66% of 400px
     }
     $dest = '/images/' . $desired_width . '_' . $src;
     jpg_thumbnail(__DIR__ . '/images/' . $src, __DIR__ . $dest, $desired_width, 200);
     if ($i % 4 === 0) {
         /* reset $i back to 0 to implement groups of 4 */
         $i = 0;
     }
 ?>
    <img src="<?php echo $dest; ?>" alt=""/>
 <?php } ?>

If you need to alter other image formats, you will need to determine the image extension and the appropriate GD image functions to use. 如果需要更改其他图像格式,则需要确定图像扩展名和要使用的适当GD图像功能。


For the CSS, I prefer to use a repeating pattern for the arrangement of the images in the parent with :nth-child . 对于CSS,我更喜欢使用:nth-child在父级中使用重复模式来排列图像。

The repeating pattern is in groups of 4 images ( 4n ). 重复图案以4张图像( 4n )为一组。 Where images 2 and 3 are 33% width and images 1 and 4 are 66% of the width. 其中图像2和3的宽度为33%,图像1和4的宽度为66%。 So we can use the pattern of 4n+x to affect the size of image x . 因此我们可以使用4n+x的模式来影响图像x的大小。

The downside to this approach, or any that uses altering widths with a set height, is that each image must be the correct aspect ratio or the image will appear to be stretched or pinched. 这种方法的缺点是,或任何使用以设定的高度改变宽度的方法,都是每个图像都必须具有正确的宽高比,否则图像将看起来被拉伸或挤压。

See: https://jsfiddle.net/o73pdxhy/1/ (or Run Snippet below) 请参阅: https//jsfiddle.net/o73pdxhy/1/ (或下面的运行代码段)

 * { box-sizing: border-box; } /* set the container to only fit 2 images */ .div-frame-five { width: 400px; padding:0; margin: 0; } /* set the spacing around each image - this reduces available image widths */ .div-frame-five img { margin: 2px; } /* define a set height for each image */ .div-frame-five img { height: 200px; } /* make images 1 and 4 in groups of 4 65% the width of div-frame-five */ .div-frame-five img:nth-child(4n+4), .div-frame-five img:nth-child(4n+1) { width: 65%; } /* make images 3 and 2 in groups of 4 32% the width of div-frame-five */ .div-frame-five img:nth-child(4n+3), .div-frame-five img:nth-child(4n+2) { width: 32%; } 
 <div class="div-frame-five"> <img src="//placehold.it/300?text=Image1" /> <img src="//placehold.it/300?text=Image2" /> <img src="//placehold.it/300?text=Image3" /> <img src="//placehold.it/300?text=Image4" /> <img src="//placehold.it/300?text=Image5" /> <img src="//placehold.it/300?text=Image6" /> <img src="//placehold.it/300?text=Image7" /> <img src="//placehold.it/300?text=Image8" /> </div> 

This will continue the pattern for any number of images in groups of 4. 这将继续以4组为一组的任意数量图像的图案。

If you want to adjust the pattern, say where the 5th image in the groups is 100% of the width. 如果要调整图案,请说出组中的第五张图像是宽度的100%。 you can change it to 5n+x and add a new css rule of img:nth-child(5n+5){ width: 100% } 您可以将其更改为5n+x并添加新的img:nth-child(5n+5){ width: 100% }规则: img:nth-child(5n+5){ width: 100% }

If you want to define the dimensions of each individual image, you can simply use :nth-child(#) . 如果要定义每个图像的尺寸,可以简单地使用:nth-child(#) Replacing # with the image placement to affect. 用要替换的图片位置替换#

For example: .div-frame-five img:nth-child(2) will affect only the second image in the <div class="div-frame-five"> . 例如: .div-frame-five img:nth-child(2)仅影响<div class="div-frame-five">的第二个图像。


To account for the disproportionation of the image aspect ratios, as seen in the previous example. 如前面的示例所示,要考虑图像纵横比的不均衡。 You would need to use image containers that include the images as backgrounds. 您将需要使用包含图像作为背景的图像容器。

The downside of this approach is that larger images will be cut off or cropped by the container. 这种方法的缺点是较大的图像将被容器裁切或裁切。

See https://jsfiddle.net/3b4hsk79/ (or run snippet below): 参见https://jsfiddle.net/3b4hsk79/ (或在下面运行代码段):

 * { box-sizing: border-box; } /* set the container to only fit 2 images */ .div-frame-five { width: 400px; padding:0; margin: 0; } /* set the spacing around each image */ .div-frame-five div { margin: 3px; } /* define a set height for each image */ .div-frame-five div { height: 200px; float: left; } /* make images 1 and 4 in groups of 4 65% the width of div-drame-five */ .div-frame-five div:nth-child(4n+4), .div-frame-five div:nth-child(4n+1) { width: 65%; } /* make images 3 and 2 in groups of 4 32% the width of div-drame-five */ .div-frame-five div:nth-child(4n+3), .div-frame-five div:nth-child(4n+2) { width: 32%; } 
 <div class="div-frame-five"> <div style="background-image: url(//placehold.it/265?text=Image1);"></div> <div style="background-image: url(//placehold.it/265?text=Image2);"></div> <div style="background-image: url(//placehold.it/265?text=Image3);"></div> <div style="background-image: url(//placehold.it/265?text=Image4);"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM