もちろん、それは可能です。その上、それはおそらく最良のアプローチです:要求によって必要なサムネイル画像を作成すること。あなたのウェブサイトが開発段階にある場合、あなたはどの次元が明日デザイナーを作成するかを推測することは決してありません。そして、アップロード機能に何度も戻って、新しいデザインに合わせます。設計とロジックの間の強い依存関係を取り除く価値があります。
codeignitorについてはよくわかりません。とにかく、テンプレートで次のようなものを使用します。
class Image {
public $filename;
public $caption;
/**
* Return full path to image.
* @return string path to file to make thumb
*/
public function fullPath() {
return "data/files/{$this->filename}";
}
/**
* Renders HTML IMG for thumb of given size.
*
* @param int $width max width, set to -1, if not important
* @param type $height max height, set to -1, if not important
* @return string html tag for image with correct width and height attributes
*/
public function htmlTag($width, $height) {
$t = $this->getThumb($width, $height);
return "<img src=\"{$t}\" alt=\"{$this->caption}\" width=\"{$width}\" height=\"{$height}\" />";
}
/**
* Get/create thumb image
* @param int $width width of the image
* @param int $height height of the image
* @return string path to the image
*/
public function getThumb(&$width, &$height) {
$currentImage = $this->fullPath();
$thumbFilename = md5($this->path . $width . $height) . '.png';
$thumbDir = 'data/thumbs/';
$thumbFilename = "{$thumbDir}/{$thumbFilename}";
// thumb already done?
if (is_file($thumbFilename)) {
// get real size to create correct html img tag
if ($width<0 || $height<0) {
$size = getimagesize($thumbFilename);
$width = $size[0];
$height = $size[1];
}
return $thumbFilename;
}
$ext = strtolower(pathinfo($currentImage, PATHINFO_EXTENSION));
if ($ext == 'jpeg' || $ext == 'jpg') {
$source = imagecreatefromjpeg($currentImage);
} else if ($ext == 'gif') {
$source = imagecreatefromgif($currentImage);
} else if ($ext == 'png') {
$source = imagecreatefrompng($currentImage);
}
$currentWidth = imagesx($source);
$currentHeight = imagesy($source);
// the sizes which we really will apply (default setup)
$realWidth = $width;
$realHeight = $height;
$realX = 0;
$realY = 0;
// decide regarding cutting
// if all params > 0, cuttin will be done
$cut = FALSE;
if ($width > 0 && $height > 0) {
$cut = TRUE;
} else if ($width < 0) { // width is not important, set proportion to that
$width = $realWidth = round($currentWidth * $height / $currentHeight);
} else if ($height < 0) { // height is not imporant
$height = $realHeight = round($currentHeight * $width / $currentWidth);
}
if ($cut) {
$kw = $currentWidth / $width;
$kh = $currentHeight / $height;
$k = $kw < $kh ? $kw : $kh;
$realWidth = round($currentWidth / $k);
$realHeight = round($currentHeight / $k);
if ($kh < $kw) {
$realX = round(($realWidth - $width) / 2) * $k;
} else {
$realY = round(($realHeight - $height) / 2) * $k;
}
}
$virtual = imagecreatetruecolor($width, $height);
imagealphablending($virtual, false);
$col = imagecolorallocatealpha($virtual, 0, 0, 0, 127);
imagefill($virtual, 0, 0, $col);
imagesavealpha($virtual, true);
imagecopyresampled($virtual, $source, 0, 0, $realX, $realY, $realWidth, $realHeight, $currentWidth, $currentHeight);
// create file
imagepng($virtual, $thumbFilename);
return $thumbFilename;
}
}
使用法:
$image = new Image();
$image->filename = "image.jpeg"; // really stored in 'data/files/image.jpg', let's say 300x400px
$image->caption = "My Image";
// get thumb 50x50: left and right parts of image will be cut off
echo $image->htmlTag(50, 50);
// get thumb of width 100 (height does not matter, keep proportions)
echo $image->htmlTag(100, -1);
// get thumb of height 100 (width does not matter, keep proportions)
echo $image->htmlTag(-1, 100);