I am working on a website that sells various illustrations, what's the best way to handle different image sizes?

I am working on a site that will allow users to upload and sell their work of various sizes. I was wondering how best to handle various file sizes automatically. A few points that I was interested in:

  • How to define different size categories (small, medium, large) so that I can dynamically resize images with proportional sizes.

  • Should I store actual jpegs of different sizes for download? Or it would be easier to create these different sizes for loading on the fly

  • My thumbnails will be slightly larger than the average thumbnails, should I store the second “thumbnail image” with watermarking sites? Or again, generate this on the fly?

All opinions, tips are greatly appreciated!

+3
source share
2 answers

I am doing something like this with aarongriffin.co.uk .

, " " , ; . , , , (, ), , , , , , " ". , .

Python Django, scol-thumbnail. PHP imagecreatefrom * , () .

( - ) .

+2

php . , .

<?php
# Constants
define(IMAGE_BASE, '/var/www/html/mbailey/images');
define(MAX_WIDTH, 150);
define(MAX_HEIGHT, 150);

# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE . "/$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
    $img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
    $img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
    $img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

    # Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

    # If the image is larger than the max shrink it
    if ($scale &lt; 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);

        # Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        # Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                         $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }
}

# Create error image if necessary
if (!$img) {
    $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
    imagecolorallocate($img,0,0,0);
    $c = imagecolorallocate($img,70,70,70);
    imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
    imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
+1

Source: https://habr.com/ru/post/1745732/


All Articles