I often need to fit an image into a specified area keeping its aspect
ratio. I use these two functions for this. Maybe you can add them to
TCPDF.
<?php
function FitAspect($w, $h, $boxw, $boxh) {
$neww = $w*($boxh/$h);
$newh = $boxh;
if ($neww > $boxw) {
$newh = $h*($boxw/$w);
$neww = $boxw;
}
return array($neww, $newh);
}
/**
* Adds an image resized with aspect ratio and aligned as specified
*
* @param string $file Filename of image (JPG/PNG)
* @param float $x horizontal position on page (user units)
* @param float $y vertical position on page (user units)
* @param float $w maximum width
* @param float $h maximum height
* @param string $type Image format (JPG, JPEG, PNG), auto-guessed on
extension
* @param string|int $link URL or link-id
* @param string $hAlign horizontal alignment inside bounding box (l,
c, r)
* @param string $vAlign veritcal alignment inside bounding box (t, m,
b)
*/
function ImageAspect($file, $x, $y, $w=0, $h=0, $type='', $link='',
$hAlign='c', $vAlign='m') {
if ($w<=0 || $h<=0) {
$this->Image($file, $x, $y, $w, $h, $type, $link);
return;
}
if ($w > $this->w) $w = $this->w;
list($imgWidth, $imgHeight, $imgType, $imgAttr) =
getimagesize($file);
list($imgNewWidth, $imgNewHeight) = $this->FitAspect($imgWidth,
$imgHeight, $w, $h);
if ($imgNewWidth<$w) {
switch ($hAlign) {
case 'r':
$x += $w-$imgNewWidth;
break;
case 'c':
$x += ($w-$imgNewWidth)/2;
break;
default:
case 'l':
break;
}
}
if ($imgNewHeight<$h) {
switch ($vAlign) {
case 'b':
$y += $h-$imgNewHeight;
break;
case 'm':
$y += ($h-$imgNewHeight)/2;
break;
default:
case 't':
break;
}
}
$this->Image($file, $x, $y, $imgNewWidth, $imgNewHeight, $type,
$link);
}
?>
Nobody/Anonymous
None
None
Public
|
Date: 2008-12-11 10:28 Current TCPDF version already contains image resizing/fitting capabilities. |
| Field | Old Value | Date | By |
|---|---|---|---|
| status_id | Open | 2008-12-11 10:28 | nicolaasuni |
| close_date | - | 2008-12-11 10:28 | nicolaasuni |