Ik heb vanmiddag eens even zitten stoeien met PHP en vroeg me af of onderstaande script een beetje goed in elkaar zit.
Ik heb hier niet de mogelijkheid om te testen, maar dat is ook niet echt de vraag. Mijn vraag is meer of ik dingen efficienter zou kunnen doen.
Hebben jullie hier nog op of aanmerkingen?
Misschien dat iemand het makkelijk vind om te gebruiken.
Ik heb hier niet de mogelijkheid om te testen, maar dat is ook niet echt de vraag. Mijn vraag is meer of ik dingen efficienter zou kunnen doen.
Hebben jullie hier nog op of aanmerkingen?
Misschien dat iemand het makkelijk vind om te gebruiken.
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
| <? # script usage: Just calling image.php without any variables display's an error # Calling the script with a filename (.jpg) returns the image exactly how it is # Calling the script with a filename, width and height will resize the original image # save it to the media dir with a prefix of the width and height an display it switch ($type) { case (!isset($type)) #Output the original image GenJPG(); case ($type == "small") # Output a thumbnail (200 x 150) GenJPG(200,150); case ($type == "medium") # Output a medium sized image (800 x 600) GenJPG(800,600); } function GenJPG($ImgWidth, $ImgHeight, $filename) { # Function to generate new images based on existing images. Only with a different size. $SmallDir = "media/200150/"; # Directory that contains the small images $MediumDir = "media/800600/"; # Directory that contains the medium images if ((!isset($filename))) { #No filename specified echo "ERROR: Filename not specified!"; } else { #Filename specified if (!isset($ImgWidth) or !isset($ImgHeight)) { #ImgWidth or ImgHeight is not set, so display the original image. Header("Content-type: image/jpeg"); $im = ImageCreateFromJPEG("media/".$filename); ImageJPEG($im,'',90); ImageDestroy($im); } else { #ImgWidth and ImgHeight is set, so check if there is allready a file with the right size #Or else generate a new image with new dimensions and save it to the appropiate folder. #Finally display it. if (is_file("media/".$ImgWidth.$ImgHeight."-".$filename) { #File allready exists. So display it right away. Header("Content-type: image/jpeg"); $im = ImageCreateFromJPEG("media/".$ImgWidth.$ImgHeight."-".$filename); ImageJPEG($im,'',90); ImageDestroy($im); } else { #BLAAT! Header("Content-type: image/jpeg"); $im = ImageCreateFromJpeg("media/".$filename); $im_tmp = ImageCreate($ImgWidth, $ImgHeight); imagecopyresized($im_tmp, $im, 0, 0, 0, 0, $ImgWidth, $ImgHeight, imagesx("media/".$filename), imagesy("media/".$filename)) imagejpeg($im_tmp, "media/".$ImgWidth.$ImgHeight."-".$filename, 90) imagejpeg($im_tmp,'', 90) imagedestroy($im) } } } } ?> |