我有一些从服务器拉出的图像,并且$imgUrl
包含图像的路径。如何用PHP缩小服务器端的图像?
现在我用<img src="<?php echo $imgUrl ?>" width="100" height="200"/>
或CSS来缩小图像,但我想这样做在PHP中,让我的发球已经缩放图像到DOM
任何想法?
感谢
我有一些从服务器拉出的图像,并且$imgUrl
包含图像的路径。如何用PHP缩小服务器端的图像?
现在我用<img src="<?php echo $imgUrl ?>" width="100" height="200"/>
或CSS来缩小图像,但我想这样做在PHP中,让我的发球已经缩放图像到DOM
任何想法?
感谢
该解决方案将导致当请求首次要创建的拇指。所有未来的请求都将获取已创建的大拇指。它是利用ImageMagick:
HTML:
<img src="script.php?img=example" />
PHP(的script.php):
$width = 140;
$height = 80;
$image = $_GET['img'];
$ext = 'png';
// Check if file exists
if (! file_exists('/path/to/the/'.$image.'.'.$ext))
{
die('Unable to process the requested file.');
}
// Check if a thumb already exists, otherwise create a thumb
if (file_exists('/path/to/the/'.$image.'_thumb.'.$ext))
{
$img = new imagick('/path/to/the/'.$image.'_thumb.'.$ext);
}
else
{
$img = new imagick('/path/to/the/'.$image.'.'.$ext);
$img->setImageFormat($ext);
$img->scaleImage($width, 0);
$img->cropImage($width, $height, 0, 0);
$img->writeImage('/path/to/the/'.$image.'_thumb.'.$ext);
}
// Return as an image
header('Content-Type: image/'.$ext);
echo $img;
要知道,在PHP这样做将意味着内存密集型过程或者每次访问该图像(如果在飞行中完成的)时间或保存图像时(这意味着你可以使用更多的存储空间来保存转换后的图像)。如果您确定这是您需要/想要的东西,那么请使用进行调查。看到这个答案的想法或如何做到这一点:Image GD resize to 100px while keep the ratio
你应该创建一个较小的版本,并将其保存到一个文件夹。那么你不需要在每次请求时重新设置它们(这是内存密集型的)。使用Gd或ImageMagick进行调整。
我有很多,我的意思是很多图像,我宁愿花一些内存,然后修改现有的代码 – Patrioticcow 2012-01-31 18:30:54
下面是我在用的自动取款机。随意提取您需要:
Usage:
<CLASS>::scale_image($dir.$name, 1024, 768, $dir.'thumb_'.$name);
/**
* Most simple way to get File Extension
* @param string $path Local path to File
* @return string Extension in lower case
*/
public static function extension($path){
return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}
/**
* Rename Filename for more secure usage
* @param string $name filename
* @return string more secure filename
*/
public static function secure_name($name){
return urlencode(preg_replace('/[^a-z0-9 \-_\.]/i', '_', strtolower($name)));
}
/**
* Scale Image without ratio changes
* @param int $sw source width of orig image
* @param int $sh source height of orig image
* @param int $tw max target width
* @param int $th max target height
* @return array list($width, $height)
*/
public static function scale($sw, $sh, $tw, $th){
if ($sw > $tw && $sw/$tw > $sh/$th) {
$tw = $sw * ($tw/$sw);
$th = $sh * ($tw/$sw);
}else if ($sh > $th) {
$tw = $sw * ($th/$sh);
$th = $sh * ($th/$sh);
}else{
$th = $sh;
$tw = $sw;
}
return array(round($tw), round($th));
}
/**
* Scale Image
* @param string $sPath
* @param int $width max width
* @param int $height max height
* @param string $tPath Optional Path for Thumbnail
* @param int $thumbScale Scale ratio for optional Thumbnail (Width and Height/$thumbScale)
* @return void
*/
public static function scale_image($sPath, $width, $height, $tPath = NULL, $thumbScale = 10){
if(!function_exists('imagecreatetruecolor')){
return;
}
$ext = strtolower(self::extension($sPath));
if($ext == 'jpg' or $ext == 'jpeg'){
$src = imagecreatefromjpeg($sPath);
}elseif($ext == 'png'){
$src = imagecreatefrompng($sPath);
}elseif($ext == 'gif'){
$src = imagecreatefromgif($sPath);
}else{
return;
}
list($sw, $sh) = getimagesize($sPath);
list($tw, $th) = File::scale($sw, $sh, $width, $height);
$trg = imagecreatetruecolor($tw, $th);
imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
imagejpeg($trg, $sPath, 90);
if($tPath){
$tw = (int)$tw/$thumbScale;
$th = (int)$th/$thumbScale;
$trg = imagecreatetruecolor($tw, $th);
imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
imagejpeg($trg, $tPath, 90);
}
}
最容易使用的工具像phpthumb:http://phpthumb.sourceforge.net/ – evan 2012-01-31 18:20:31
确保你做这个* *网页会被要求先而非点播,作为 - 使网页执行速度变慢,从而使网站变慢。 – Incognito 2012-01-31 18:22:45
@evan如果你已经回答,我会upvote。除非OP真的想写他/她自己的图像处理库,我认为这会在php中造成痛苦。 – Tim 2012-01-31 18:22:57