Search code examples
phpimageurljoomlaresize

how to resize image from url and make the size of the image smaller


I have a joomla website with the virtuemart module.

What I have are images and thumbnails which are hosted on a other domain.

Virtuemart gives me 2 options:

1 - browse for image, a thumbnail will be automaticly created

2 - enter a URL for a image which is external hosted, this option doesn't have a resize option

The way I have overcome this problem (with help ofcourse) is to set a height="" attribute to the img tag. The only problem is that when a big image is loaded ie: 500 x 700 px, the so called thumbnail takes time to load, this is logical.

Can someone give me options how a image from a url can be "resized" with as outcome that the thumbnail takes less time to load?

Personally I was thinking about a way to decrease the image quality of the resized image which comes from a url with a code, css or anything else?

I know this has less to do with solving a code, but I know you guys know more options then I do.


Solution

  • You can make use of imagecopyresampled function of PHP.

    Sample program (source: php.net)

    <?php
    // The file
    $filename = 'http://valplibrary.files.wordpress.com/2009/01/5b585d_merry-christmas-blue-style.jpg';
    $percent = 0.5; // percentage of resize
    
    // Content type
    header('Content-type: image/jpeg');
    
    // Get new dimensions
    list($width, $height) = getimagesize($filename);
    $new_width = $width * $percent;
    $new_height = $height * $percent;
    
    // Resample
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    // Output
    imagejpeg($image_p, null, 100);
    ?>