瀏覽代碼

Resize the image if it size is greater than 500px

pecesama 18 年之前
父節點
當前提交
4a6e3eb765
共有 2 個文件被更改,包括 112 次插入3 次删除
  1. 91 0
      classes/imgsize.php
  2. 21 3
      index.php

+ 91 - 0
classes/imgsize.php 查看文件

@@ -0,0 +1,91 @@
1
+<?php
2
+header ("Content-type: image/jpeg");
3
+/*
4
+JPEG / PNG Image Resizer
5
+Parameters (passed via URL):
6
+
7
+img = path / url of jpeg or png image file
8
+
9
+percent = if this is defined, image is resized by it's
10
+          value in percent (i.e. 50 to divide by 50 percent)
11
+
12
+w = image width
13
+
14
+h = image height
15
+
16
+constrain = if this is parameter is passed and w and h are set
17
+            to a size value then the size of the resulting image
18
+            is constrained by whichever dimension is smaller
19
+
20
+Requires the PHP GD Extension
21
+
22
+Outputs the resulting image in JPEG Format
23
+
24
+By: Michael John G. Lopez - www.sydel.net
25
+Filename : imgsize.php
26
+*/
27
+
28
+$img = $_GET['img'];
29
+$percent = $_GET['percent'];
30
+$constrain = $_GET['constrain'];
31
+$w = $_GET['w'];
32
+$h = $_GET['h'];
33
+
34
+// get image size of img
35
+$x = @getimagesize($img);
36
+// image width
37
+$sw = $x[0];
38
+// image height
39
+$sh = $x[1];
40
+
41
+if ($percent > 0) {
42
+	// calculate resized height and width if percent is defined
43
+	$percent = $percent * 0.01;
44
+	$w = $sw * $percent;
45
+	$h = $sh * $percent;
46
+} else {
47
+	if (isset ($w) AND !isset ($h)) {
48
+		// autocompute height if only width is set
49
+		$h = (100 / ($sw / $w)) * .01;
50
+		$h = @round ($sh * $h);
51
+	} elseif (isset ($h) AND !isset ($w)) {
52
+		// autocompute width if only height is set
53
+		$w = (100 / ($sh / $h)) * .01;
54
+		$w = @round ($sw * $w);
55
+	} elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
56
+		// get the smaller resulting image dimension if both height
57
+		// and width are set and $constrain is also set
58
+		$hx = (100 / ($sw / $w)) * .01;
59
+		$hx = @round ($sh * $hx);
60
+
61
+		$wx = (100 / ($sh / $h)) * .01;
62
+		$wx = @round ($sw * $wx);
63
+
64
+		if ($hx < $h) {
65
+			$h = (100 / ($sw / $w)) * .01;
66
+			$h = @round ($sh * $h);
67
+		} else {
68
+			$w = (100 / ($sh / $h)) * .01;
69
+			$w = @round ($sw * $w);
70
+		}
71
+	}
72
+}
73
+
74
+$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
75
+$im = @ImageCreateFromPNG ($img) or // or PNG Image
76
+$im = @ImageCreateFromGIF ($img) or // or GIF Image
77
+$im = false; // If image is not JPEG, PNG, or GIF
78
+
79
+if (!$im) {
80
+	// We get errors from PHP's ImageCreate functions...
81
+	// So let's echo back the contents of the actual image.
82
+	readfile ($img);
83
+} else {
84
+	// Create the resized image destination
85
+	$thumb = @ImageCreateTrueColor ($w, $h);
86
+	// Copy from image source, resize it, and paste to image destination
87
+	@ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
88
+	// Output resized image
89
+	@ImageJPEG ($thumb);
90
+}
91
+?>

+ 21 - 3
index.php 查看文件

@@ -82,9 +82,18 @@
82 82
 						$template->cargarPlantilla($input, $output, "template_regular_post");
83 83
 						$template->mostrarPlantilla();
84 84
 						break;
85
-					case "2":
85
+					case "2":						
86
+						$fileName = "uploads/".getFileName($register["url"]);
87
+						
88
+						$x = @getimagesize($fileName);						
89
+						if ($x[0] > 500) {							
90
+							$photoPath = $conf->urlGelato."/classes/imgsize.php?w=500&img=".$register["url"];
91
+						} else {
92
+							$photoPath = $register["url"];
93
+						}
94
+						
86 95
 						$input = array("{Date_Added}", "{Permalink}", "{PhotoURL}", "{PhotoAlt}", "{Caption}", "{URL_Tumble}");
87
-						$output = array($formatedDate, $permalink, $register["url"], "", $register["description"], $conf->urlGelato);
96
+						$output = array($formatedDate, $permalink, $photoPath, "", $register["description"], $conf->urlGelato);
88 97
 						
89 98
 						$template->cargarPlantilla($input, $output, "template_photo");
90 99
 						$template->mostrarPlantilla();							   
@@ -147,8 +156,17 @@
147 156
 				$template->mostrarPlantilla();
148 157
 				break;
149 158
 			case "2":
159
+				$fileName = "uploads/".getFileName($register["url"]);
160
+						
161
+				$x = @getimagesize($fileName);						
162
+				if ($x[0] > 500) {					
163
+					$photoPath = $conf->urlGelato."/classes/imgsize.php?w=500&img=".$register["url"];
164
+				} else {
165
+					$photoPath = $register["url"];
166
+				}
167
+				
150 168
 				$input = array("{Date_Added}", "{Permalink}", "{PhotoURL}", "{PhotoAlt}", "{Caption}", "{URL_Tumble}");
151
-				$output = array($formatedDate, $permalink, $register["url"], "", $register["description"], $conf->urlGelato);
169
+				$output = array($formatedDate, $permalink, $photoPath, "", $register["description"], $conf->urlGelato);
152 170
 				
153 171
 				$template->cargarPlantilla($input, $output, "template_photo");
154 172
 				$template->mostrarPlantilla();