Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...