jrobinsonc
1/23/2016 - 7:48 PM

Wordpress helper: create_thumb.

Wordpress helper: create_thumb.

<?php

/**
 * create_thumb
 * 
 * @author JoseRobinson.com
 * @link https://gist.github.com/jrobinsonc/b40c99a4d688f8a2d554
 * @param string $file_src_path
 * @param int $width
 * @param int $height
 * @param bool $crop
 * @return array
 */
function create_thumb($file_src_path, $width, $height, $crop = false)
{
    $upload_dir = wp_upload_dir();
    $ds = DIRECTORY_SEPARATOR;


    /**
     * If the path is an absolute path:
     */
    if (is_file($file_src_path))
    {
        $file_path = $file_src_path;
    }
    /**
     * Or, if is a relative path:
     */
    elseif (is_file($file_src_path_absolute = $upload_dir['basedir'] . $ds . ltrim($file_src_path, $ds)))
    {
        $file_path = $file_src_path_absolute;
    }
    // If is an URL:
    elseif (filter_var($file_src_path, FILTER_VALIDATE_URL) !== false)
    {
        $path_info = parse_url($file_src_path);

        /**
         * If the image is in this server:
         */
        if (isset($path_info['host']) && $path_info['host'] === $_SERVER['HTTP_HOST'])
        {
            /**
             * We only need to change the url for the path.
             */
            if (strpos($file_src_path, $upload_dir['baseurl']) !== false)
            {
                $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_src_path);

                if (!file_exists($file_path))
                    return false;
            }
            else
            {
                goto download_image;
            }
        }
        /**
         * If is outside:
         */
        else
        {
            download_image:

            /**
             * We need to download the image to this server.
             */
            
            // This is the path for the new image:
            $file_path = $upload_dir['path'] . $ds . wp_unique_filename($upload_dir['path'], basename($file_src_path));

            $fi = fopen($file_src_path, 'rb');
            $fo = fopen($file_path, 'wb');

            while(!feof($fi))
                fwrite($fo, fread($fi, 65536));

            fclose($fi);
            fclose($fo);
            clearstatcache();
        }
    }


    // -------------------------------------------------------------
    if (!isset($file_path) || !file_exists($file_path))
        return false;

    $image = wp_get_image_editor($file_path);

    if (is_wp_error($image))
        return false;


    $file_src_size = $image->get_size();
    $file_dest_size = image_resize_dimensions( $file_src_size['width'], $file_src_size['height'], $width, $height, $crop );
    $file_dest_size['width'] = $file_dest_size[4];
    $file_dest_size['height'] = $file_dest_size[5];

    $thumb_file_path = preg_replace('#^(.+)\.(png|jpe?g|gif)$#i', "$1-{$file_dest_size['width']}x{$file_dest_size['height']}.$2", "{$file_path}");

    if (is_file($thumb_file_path))
    {
        $output = [
            'path' => $thumb_file_path,
            'file' => basename($thumb_file_path),
            'width' => $file_dest_size['width'],
            'height' => $file_dest_size['height'],
            'mime-type' => wp_check_filetype(basename($thumb_file_path))['type'],
        ];
    }
    else
    {
        $image->resize($width, $height, $crop);
        $output = $image->save();
    }

    $output['url'] = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $output['path']);

    
    return $output;
}