function-imageresize
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
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
<?php
function image_resize($image,$width,$height,$color=array(0,0,0)){
if(@$size = getimagesize($image)){
$newsize=array();
if($size[0]==$size[1]){
$newsize['width']=$width;
$newsize['height']=$height;
}elseif($size[0]<$size[1]){
$ratio=$height/$size[1];
$newsize['width']=$ratio*$size[0];
$newsize['height']=$ratio*$size[1];
}else{
$ratio=$width/$size[0];
$newsize['width']=$ratio*$size[0];
$newsize['height']=$ratio*$size[1];
}
if($newsize['width']>$size[0]){
$newsize['width']=$size[0];
}
if($newsize['height']>$size[1]){
$newsize['height']=$size[1];
}
switch($size[2]){
case 1:
$input=imagecreatefromgif($image);
break;
case 2:
$input=imagecreatefromjpeg($image);
break;
case 3:
$input=imagecreatefrompng(image);
break;
default:
$input=imagecreate($width,$height);
imagecolorallocate($input,125,125,125);
imagestring($input,2,2,2,'Wrong image type',imagecolorallocate($input,230,230,230));
}
$output=imagecreatetruecolor($width,$height);
imagecolorallocate($output,$color[0],$color[1],$color[2]);
imagecopyresampled($output,$input,0,0,0,0,$newsize['width'],$newsize['height'],$size[0],$size[1]);
}else{
$output=imagecreate($width,$height);
imagecolorallocate($output,125,125,125);
imagestring($output,2,2,2,'The image does not exists',imagecolorallocate($output,230,230,230));
imagestring($output,2,2,15,'or is not reachable',imagecolorallocate($output,230,230,230));
}
imagepng($output);
}
image_resize('http://website.com/pic.gif',100,200,array(255,0,0));
/* de eerste waarde is de url naar het plaatje
de 2e de breedte
de 3e is de hoogte
en de 4e de kleuren (in RGB) */
?>
function image_resize($image,$width,$height,$color=array(0,0,0)){
if(@$size = getimagesize($image)){
$newsize=array();
if($size[0]==$size[1]){
$newsize['width']=$width;
$newsize['height']=$height;
}elseif($size[0]<$size[1]){
$ratio=$height/$size[1];
$newsize['width']=$ratio*$size[0];
$newsize['height']=$ratio*$size[1];
}else{
$ratio=$width/$size[0];
$newsize['width']=$ratio*$size[0];
$newsize['height']=$ratio*$size[1];
}
if($newsize['width']>$size[0]){
$newsize['width']=$size[0];
}
if($newsize['height']>$size[1]){
$newsize['height']=$size[1];
}
switch($size[2]){
case 1:
$input=imagecreatefromgif($image);
break;
case 2:
$input=imagecreatefromjpeg($image);
break;
case 3:
$input=imagecreatefrompng(image);
break;
default:
$input=imagecreate($width,$height);
imagecolorallocate($input,125,125,125);
imagestring($input,2,2,2,'Wrong image type',imagecolorallocate($input,230,230,230));
}
$output=imagecreatetruecolor($width,$height);
imagecolorallocate($output,$color[0],$color[1],$color[2]);
imagecopyresampled($output,$input,0,0,0,0,$newsize['width'],$newsize['height'],$size[0],$size[1]);
}else{
$output=imagecreate($width,$height);
imagecolorallocate($output,125,125,125);
imagestring($output,2,2,2,'The image does not exists',imagecolorallocate($output,230,230,230));
imagestring($output,2,2,15,'or is not reachable',imagecolorallocate($output,230,230,230));
}
imagepng($output);
}
image_resize('http://website.com/pic.gif',100,200,array(255,0,0));
/* de eerste waarde is de url naar het plaatje
de 2e de breedte
de 3e is de hoogte
en de 4e de kleuren (in RGB) */
?>