Skip to content

Commit 4363b57

Browse files
committed
updating local files
0 parents  commit 4363b57

File tree

9 files changed

+2310
-0
lines changed

9 files changed

+2310
-0
lines changed

HtmlDomHandler.php

Lines changed: 649 additions & 0 deletions
Large diffs are not rendered by default.

ImageHandler.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/*
3+
* Copyright (c) 2025 Bloxtor (http://bloxtor.com) and Joao Pinto (http://jplpinto.com)
4+
*
5+
* Multi-licensed: BSD 3-Clause | Apache 2.0 | GNU LGPL v3 | HLNC License (http://bloxtor.com/LICENSE_HLNC.md)
6+
* Choose one license that best fits your needs.
7+
*
8+
* Original PHP Html Dom Lib Repo: https://github.com/a19836/phphtmldomlib/
9+
* Original Bloxtor Repo: https://github.com/a19836/bloxtor
10+
*
11+
* YOU ARE NOT AUTHORIZED TO MODIFY OR REMOVE ANY PART OF THIS NOTICE!
12+
*/
13+
14+
include __DIR__ . "/MimeTypeHandler.php";
15+
16+
class ImageHandler {
17+
const IMAGE_DOES_NOT_EXIST = 1001;//Unsupported picture type!
18+
const UNSUPPORTED_IMAGE_TYPE = 1002;//Unsupported picture type!
19+
const IMAGE_IS_TOO_SMALL = 1003;//Picture is too small!
20+
const IMAGE_WITH_WRONG_ASPECT_RATIO = 1004;//Picture has wrong aspect ratio
21+
22+
private $errors = array();
23+
private $allowed_extensions = array('bmp', 'gif', 'jpeg', 'jpg', 'png');
24+
25+
public function __construct() {
26+
27+
}
28+
29+
public function getErrors() {
30+
return $this->errors;
31+
}
32+
33+
public function isImageValid($src_path) {
34+
$this->errors = array();
35+
36+
if (!$src_path || !file_exists($src_path)) {
37+
$this->errors[] = self::IMAGE_DOES_NOT_EXIST;
38+
return false;
39+
}
40+
41+
$mime_type = MimeTypeHandler::getFileMimeType($src_path);
42+
43+
if (!MimeTypeHandler::isImageMimeType($mime_type)) {
44+
$this->errors[] = self::UNSUPPORTED_IMAGE_TYPE;
45+
return false;
46+
}
47+
48+
$type = MimeTypeHandler::getTypeByMimeType($mime_type);
49+
$extension = $type && isset($type["extension"]) ? $type["extension"] : substr(strrchr($src_path, "."), 1);
50+
$parts = explode(",", $extension);
51+
$extension = trim(strtolower($parts[0]));
52+
53+
$allowed_extensions = MimeTypeHandler::getAvailableFileExtensions("image");
54+
$allowed_extensions = is_array($allowed_extensions) && $allowed_extensions ? $allowed_extensions : $this->allowed_extensions;
55+
56+
return in_array($extension, $allowed_extensions);
57+
}
58+
59+
public function isImageBinaryValid($src_path) {
60+
$status = $this->isImageValid($src_path);
61+
62+
if ($status && getimagesize($src_path) === false) {
63+
$this->errors[] = self::UNSUPPORTED_IMAGE_TYPE;
64+
$status = false;
65+
}
66+
67+
return $status;
68+
}
69+
70+
public function imageResize($src_path, $dst_path, $width, $height, $crop = false, $force_resize = false) {
71+
if (!$this->isImageBinaryValid($src_path))
72+
return false;
73+
74+
$status = false;
75+
$extension = $this->getFileExtension($src_path);
76+
77+
switch($extension){
78+
case 'bmp':
79+
$img = imagecreatefromwbmp($src_path);
80+
break;
81+
case 'gif':
82+
$img = imagecreatefromgif($src_path);
83+
break;
84+
case 'jpeg':
85+
case 'jpg':
86+
$img = imagecreatefromjpeg($src_path);
87+
break;
88+
case 'png':
89+
$img = imagecreatefrompng($src_path);
90+
break;
91+
default :
92+
$this->errors[] = self::UNSUPPORTED_IMAGE_TYPE;
93+
return false;
94+
}
95+
96+
if ($img) {
97+
list($w, $h) = getimagesize($src_path);
98+
99+
if ($w != $width || $h != $height) {
100+
if (($width > $height && $w < $h) || ($width < $height && $w > $h))
101+
$this->errors[] = self::IMAGE_WITH_WRONG_ASPECT_RATIO;
102+
103+
// resize
104+
if($crop) {
105+
if($w < $width || $h < $height)
106+
$this->errors[] = self::IMAGE_IS_TOO_SMALL;
107+
108+
$ratio = max($width / $w, $height / $h);
109+
$h = $height / $ratio;
110+
$x = ($w - $width / $ratio) / 2;
111+
$w = $width / $ratio;
112+
}
113+
else {
114+
if($w < $width && $h < $height)
115+
$this->errors[] = self::IMAGE_IS_TOO_SMALL;
116+
117+
$ratio = min($width / $w, $height / $h);
118+
$width = $w * $ratio;
119+
$height = $h * $ratio;
120+
$x = 0;
121+
}
122+
123+
//only resize if image is not too small, otherwise is not worth it.
124+
if (!in_array(self::IMAGE_IS_TOO_SMALL, $this->errors) || $force_resize) {
125+
$new = imagecreatetruecolor($width, $height);
126+
127+
if ($new === false)
128+
return false;
129+
130+
// preserve transparency
131+
if($extension == "gif" || $extension == "png") {
132+
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
133+
imagealphablending($new, false);
134+
imagesavealpha($new, true);
135+
}
136+
137+
if (imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h))
138+
switch($extension){
139+
case 'bmp':
140+
$status = imagewbmp($new, $dst_path);
141+
break;
142+
case 'gif':
143+
$status = imagegif($new, $dst_path);
144+
break;
145+
case 'jpeg':
146+
case 'jpg':
147+
$status = imagejpeg($new, $dst_path);
148+
break;
149+
case 'png':
150+
$status = imagepng($new, $dst_path);
151+
break;
152+
}
153+
154+
if (function_exists("imagedestroy"))
155+
@imagedestroy($new);
156+
}
157+
else //if is smaller than the user width/height, simply copy image to $dst_path
158+
$status = copy($src_path, $dst_path);
159+
}
160+
else //if width and height are the same, simply copy image to $dst_path
161+
$status = copy($src_path, $dst_path);
162+
163+
if (function_exists("imagedestroy"))
164+
@imagedestroy($img);
165+
}
166+
167+
return $status;
168+
}
169+
170+
public function areImageMeasuresValid($src_path, $width, $height, $crop = false) {
171+
if (!$this->isImageBinaryValid($src_path))
172+
return false;
173+
174+
list($w, $h) = getimagesize($src_path);
175+
176+
if($crop && ($w < $width || $h < $height)) {
177+
$this->errors[] = self::IMAGE_IS_TOO_SMALL;
178+
return false;
179+
}
180+
else if(!$crop && $w < $width && $h < $height) {
181+
$this->errors[] = self::IMAGE_IS_TOO_SMALL;
182+
return false;
183+
}
184+
185+
if (($width > $height && $w < $h) || ($width < $height && $w > $h)) {
186+
$this->errors[] = self::IMAGE_WITH_WRONG_ASPECT_RATIO;
187+
return false;
188+
}
189+
190+
return true;
191+
}
192+
193+
private function getFileExtension($src_path) {
194+
$mime_type = MimeTypeHandler::getFileMimeType($src_path);
195+
$type = MimeTypeHandler::getTypeByMimeType($mime_type);
196+
$extension = $type && isset($type["extension"]) ? $type["extension"] : substr(strrchr($src_path, "."), 1);
197+
$parts = explode(",", $extension);
198+
return trim(strtolower($parts[0]));
199+
}
200+
}
201+
202+
/*
203+
$pic_type = strtolower(strrchr($picture['name'],"."));
204+
$pic_name = "original$pic_type";
205+
move_uploaded_file($picture['tmp_name'], $pic_name);
206+
if (true !== ($pic_error = @ImageHandler::imageResize($pic_name, "100x100$pic_type", 100, 100, 1))) {
207+
echo $pic_error;
208+
unlink($pic_name);
209+
}
210+
else echo "OK!";
211+
*/
212+
?>

LICENSE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This is **multi-licensed**. You may choose to use it under **one of the following licenses**:
2+
3+
- **BSD 3-Clause License** or
4+
- **Apache License 2.0** or
5+
- **GNU LGPL v3** or
6+
- **[HLNC License](http://bloxtor.com/LICENSE_HLNC.md)**
7+
8+
Select the license that best fits your needs.
9+
10+
**This is 100% open to your needs!**
11+
12+
© 2025 [Bloxtor](http://bloxtor.com) and [Joao Pinto](http://jplpinto.com)

0 commit comments

Comments
 (0)