K
K
Kepler542016-06-22 16:24:23
CodeIgniter
Kepler54, 2016-06-22 16:24:23

How to remove from the CROP function?

Hello everyone, I have a script on the engine that creates thumbnail files,
but I want to change it, firstly, you need to remove the CROP function, and leave only resize,
and make sure that the height is auto, that is, width: 350px and height something that fits the width by aspect ratio.
I couldn't myself as I am new to PHP, I would be very grateful if you could help me. Below is the complete function code

if ( ! function_exists('create_aspect_ratio_thumb'))
{
    function create_rectangle_thumb($img,$dest)
    {
        $seg = explode('.',$img);
        $thumbType        = 'jpg';
        $thumbSize        = 300;
        $thumbWidth     = 350;
        $thumbPath        = $dest;
        $thumbQuality     = 90;

        $last_index = count($seg);
        $last_index--;

        if(strcasecmp($seg[$last_index], 'jpg') == 0 || strcasecmp($seg[$last_index], 'jpeg') == 0)
        {
            if (!$full = imagecreatefromjpeg($img)) {
                return 'error';
            }
        }
        else if(strcasecmp($seg[$last_index], 'png') == 0)
        {
            if (!$full = imagecreatefrompng($img)) {
                return 'error';
            }
        }
        else if(strcasecmp($seg[$last_index], 'gif') == 0)
        {
            if (!$full = imagecreatefromgif($img)) {
                return 'error';
            }
        }

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

        /* work out the smaller version, setting the shortest side to the size of the thumb, constraining height/wight */
        if ($height > $width) {
            $divisor = $width / $thumbHeight;
        } else {
            $divisor = $height / $thumbWidth;
        }

        $resizedWidth   = ceil($width / $divisor);
        $resizedHeight  = ceil($height / $divisor);

        /* work out center point */
        $thumbx = floor(($resizedWidth  - $thumbWidth) / 2);
        $thumby = floor(($resizedHeight - $thumbHeight) / 2);

        /* create the small smaller version, then crop it centrally to create the thumbnail */
        $resized  = imagecreatetruecolor($resizedWidth, $resizedHeight);
        $thumb    = imagecreatetruecolor($thumbWidth, $thumbHeight);
        imagecopyresized($resized, $full, 0, 0, 0, 0, $resizedWidth, $resizedHeight, $width, $height);
        imagecopyresized($thumb, $resized, 0, 0, $thumbx, $thumby, $thumbWidth, $thumbHeight, $thumbWidth, $thumbHeight);

        $name = name_from_url($img);

        imagejpeg($thumb, $thumbPath.str_replace('_large.jpg', '_thumb.jpg', $name), $thumbQuality);
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Bukharev, 2016-06-22
@evgenybuckharev

Use ready-made proven solution https://github.com/verot/class.upload.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question