Donate Share

TCPDF - PHP class for PDF

Tracker: Feature Requests

5 Image fitting - ID: 2059906
Last Update: Settings changed ( nicolaasuni )

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


Markus Birth ( rbriddick ) - 2008-08-19 14:49

5

Closed

None

Nobody/Anonymous

None

None

Public


Comment ( 1 )

Date: 2008-12-11 10:28
Sender: nicolaasuniProject AdminAccepting Donations

Current TCPDF version already contains image resizing/fitting capabilities.


Attached File

No Files Currently Attached

Changes ( 2 )

Field Old Value Date By
status_id Open 2008-12-11 10:28 nicolaasuni
close_date - 2008-12-11 10:28 nicolaasuni