Last active
July 28, 2020 04:53
-
-
Save thomasdesmoulin/398886164ffd0566f6fa3b3171bb86f4 to your computer and use it in GitHub Desktop.
scale and align image with FPDF #uniformToFill #scale #fit #fpdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require("fpdf.php"); | |
class Pdf extends \FPDF | |
{ | |
const DPI = 300; | |
const MM_IN_INCH = 25.4; | |
const A4_HEIGHT = 297; | |
const A4_WIDTH = 210; | |
/** | |
* Resize et fit une image image | |
* | |
* @param string $imgPath | |
* @param integer $x | |
* @param integer $y | |
* @param integer $containerWidth | |
* @param integer $containerHeight | |
* @param string $alignment | |
* @return void | |
*/ | |
public function imageUniformToFill(string $imgPath, int $x = 0, int $y = 0, int $containerWidth = 210, int $containerHeight = 297, string $alignment = 'C') | |
{ | |
list($width, $height) = $this->resizeToFit($imgPath, $containerWidth, $containerHeight); | |
if ($alignment === 'R') | |
{ | |
$this->Image($imgPath, $x+$containerWidth-$width, $y+($containerHeight-$height)/2, $width, $height); | |
} | |
else if ($alignment === 'B') | |
{ | |
$this->Image($imgPath, $x, $y+$containerHeight-$height, $width, $height); | |
} | |
else if ($alignment === 'C') | |
{ | |
$this->Image($imgPath, $x+($containerWidth-$width)/2, $y+($containerHeight-$height)/2, $width, $height); | |
} | |
else | |
{ | |
$this->Image($imgPath, $x, $y, $width, $height); | |
} | |
} | |
/** | |
* Convertit des pixels en mm | |
* | |
* @param integer $val | |
* @return integer | |
*/ | |
protected function pixelsToMm(int $val) : int | |
{ | |
return (int)(round($val * $this::MM_IN_INCH / $this::DPI)); | |
} | |
/** | |
* Convertit des mm en pixels | |
* | |
* @param integer $val | |
* @return integer | |
*/ | |
protected function mmToPixels(int $val) : int | |
{ | |
return (int)(round($this::DPI * $val / $this::MM_IN_INCH)); | |
} | |
/** | |
* Resize une image | |
* | |
* @param string $imgPath | |
* @param integer $maxWidth en mm | |
* @param integer $maxHeight en mm | |
* @return int[] | |
*/ | |
protected function resizeToFit(string $imgPath, int $maxWidth = 210, int $maxHeight = 297) : array | |
{ | |
list($width, $height) = getimagesize($imgPath); | |
$widthScale = $this->mmtopixels($maxWidth) / $width; | |
$heightScale = $this->mmToPixels($maxHeight) / $height; | |
$scale = min($widthScale, $heightScale); | |
return array( | |
$this->pixelsToMM($scale * $width), | |
$this->pixelsToMM($scale * $height) | |
); | |
} | |
} | |
// usage: | |
$pdf = new Pdf(); | |
$pdf->AddPage(); | |
$pdf->imageUniformToFill($imgPath, $x, $y ,$containerWidth, $containerHeight, $alignment) //$alignment "B", "T", "L", "R", "C" | |
$pdf->Output(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merci à https://gist.github.com/benshimmin/4088493