thumbnail creator
Hoi,
Ik heb een script nodig die afbeeldingen converteert naar een resolutie van 160px breed 100px hoog en die de aspect ration niet verandert maar gewoon witte borders toevoegt. Ik heb deze code gevonden:
Maar die voegt de witte borders (volgens mij ook wel canvas genoemd) niet toe.
Iemand een idee, script, of tip? :)
Ik heb een script nodig die afbeeldingen converteert naar een resolutie van 160px breed 100px hoog en die de aspect ration niet verandert maar gewoon witte borders toevoegt. Ik heb deze code gevonden:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
class thumbnailGenerator {
var $allowableTypes = array(
IMAGETYPE_GIF,
IMAGETYPE_JPEG,
IMAGETYPE_PNG
);
public function imageCreateFromFile($filename, $imageType) {
switch($imageType) {
case IMAGETYPE_GIF : return imagecreatefromgif($filename);
case IMAGETYPE_JPEG : return imagecreatefromjpeg($filename);
case IMAGETYPE_PNG : return imagecreatefrompng($filename);
default : return false;
}
}
public function generate($sourceFilename, $maxWidth, $maxHeight, $targetFormatOrFilename = 'jpg') {
$size = getimagesize($sourceFilename);
if(!in_array($size[2], $this->allowableTypes)) {
return false;
}
$pathinfo = pathinfo($targetFormatOrFilename);
if($pathinfo['basename'] == $pathinfo['filename']) {
$extension = strtolower($targetFormatOrFilename);
$targetFormatOrFilename = null;
}
else {
$extension = strtolower($pathinfo['extension']);
}
switch($extension) {
case 'gif' : $function = 'imagegif'; break;
case 'png' : $function = 'imagepng'; break;
default : $function = 'imagejpeg'; break;
}
$source = $this->imageCreateFromFile($sourceFilename, $size[2]);
if(!$source) {
return false;
}
if($targetFormatOrFilename == null) {
if($extension == 'jpg') {
header("Content-Type: image/jpeg");
}
else {
header("Content-Type: image/$extension");
}
}
if($size[0] <= $maxWidth && $size[1] <= $maxHeight) {
$function($source, $targetFormatOrFilename);
}
else {
$ratioWidth = $maxWidth / $size[0];
$ratioHeight = $maxHeight / $size[1];
if($ratioWidth < $ratioHeight) {
$newWidth = $maxWidth;
$newHeight = round($size[1] * $ratioWidth);
}
else {
$newWidth = round($size[0] * $ratioHeight);
$newHeight = $maxHeight;
}
$target = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($target, $source, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
$function($target, $targetFormatOrFilename);
}
return true;
}
}
?>
class thumbnailGenerator {
var $allowableTypes = array(
IMAGETYPE_GIF,
IMAGETYPE_JPEG,
IMAGETYPE_PNG
);
public function imageCreateFromFile($filename, $imageType) {
switch($imageType) {
case IMAGETYPE_GIF : return imagecreatefromgif($filename);
case IMAGETYPE_JPEG : return imagecreatefromjpeg($filename);
case IMAGETYPE_PNG : return imagecreatefrompng($filename);
default : return false;
}
}
public function generate($sourceFilename, $maxWidth, $maxHeight, $targetFormatOrFilename = 'jpg') {
$size = getimagesize($sourceFilename);
if(!in_array($size[2], $this->allowableTypes)) {
return false;
}
$pathinfo = pathinfo($targetFormatOrFilename);
if($pathinfo['basename'] == $pathinfo['filename']) {
$extension = strtolower($targetFormatOrFilename);
$targetFormatOrFilename = null;
}
else {
$extension = strtolower($pathinfo['extension']);
}
switch($extension) {
case 'gif' : $function = 'imagegif'; break;
case 'png' : $function = 'imagepng'; break;
default : $function = 'imagejpeg'; break;
}
$source = $this->imageCreateFromFile($sourceFilename, $size[2]);
if(!$source) {
return false;
}
if($targetFormatOrFilename == null) {
if($extension == 'jpg') {
header("Content-Type: image/jpeg");
}
else {
header("Content-Type: image/$extension");
}
}
if($size[0] <= $maxWidth && $size[1] <= $maxHeight) {
$function($source, $targetFormatOrFilename);
}
else {
$ratioWidth = $maxWidth / $size[0];
$ratioHeight = $maxHeight / $size[1];
if($ratioWidth < $ratioHeight) {
$newWidth = $maxWidth;
$newHeight = round($size[1] * $ratioWidth);
}
else {
$newWidth = round($size[0] * $ratioHeight);
$newHeight = $maxHeight;
}
$target = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($target, $source, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
$function($target, $targetFormatOrFilename);
}
return true;
}
}
?>
Maar die voegt de witte borders (volgens mij ook wel canvas genoemd) niet toe.
Iemand een idee, script, of tip? :)
na regel 61:
Dan heb je ruimte gecreeërd op je canvas voor de witte borders.
Misschien dat je de kleur wit nog wel zelf even moet intekenen.
Code (php)
1
2
3
4
2
3
4
<?php
$source_new = $target;
imagecopy($target, $source_new, 0, 0, $src_x , $src_y , $src_w , $src_h);
?>
$source_new = $target;
imagecopy($target, $source_new, 0, 0, $src_x , $src_y , $src_w , $src_h);
?>
Dan heb je ruimte gecreeërd op je canvas voor de witte borders.
Misschien dat je de kleur wit nog wel zelf even moet intekenen.




