GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — added initial src file and composer.json · codeurs/ImageHelper@4981ebe

This repository was archived by the owner on Dec 16, 2022. It is now read-only.

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .gitignore
  • composer.json
  • src
    • ImageHelper.php
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
vendor/
Collapse file

‎composer.json‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "codeurs/imagehelper",
3+
"description": "A wrapper around the popular Intervention Image php library, for common image manipulations extended with caching.",
4+
"authors": [
5+
{
6+
"name": "Codeurs",
7+
"email": "[email protected]"
8+
}
9+
],
10+
"minimum-stability": "dev",
11+
"require": {
12+
"php": ">=5.3.0",
13+
"intervention/image": "^2.3"
14+
},
15+
"autoload": {
16+
"psr-0": {
17+
"ImageHelper": "src/"
18+
}
19+
}
20+
}
Collapse file

‎src/ImageHelper.php‎

Copy file name to clipboard
+128Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
use Intervention\Image\ImageManagerStatic as Image;
4+
5+
class ImageHelper{
6+
private $image;
7+
private $quality;
8+
9+
private $originalPath;
10+
private $path;
11+
private $extension;
12+
13+
private $handler;
14+
private $operations = [];
15+
16+
/** @return ImageHelper */
17+
public static function fromCMS($handler){
18+
return new ImageHelper(str_replace('/admin/', 'admin/', $handler->src()));
19+
}
20+
21+
/** @return ImageHelper */
22+
public static function fromPath($path){
23+
return new ImageHelper($path);
24+
}
25+
26+
public function __construct($path, $quality = 65){
27+
$this->quality = $quality;
28+
$this->originalPath = $path;
29+
$this->extension = pathinfo($path, PATHINFO_EXTENSION);
30+
$pathPieces = explode('/', rtrim($path, '/'));
31+
$this->path = substr(array_pop($pathPieces), 0, -strlen($this->extension)-1) . 'q' . $quality;
32+
}
33+
34+
/** @return ImageHelper */
35+
public function resize($width, $height, $preventUpsize = true){
36+
if(!$width && !$height) throw new Exception("Either width or height has to be defined");
37+
38+
$this->path .= '_resize' . ($width?$width:'a') . 'x' . ($height?$height:'a');
39+
$this->operations[] = function($im) use ($width, $height, $preventUpsize){
40+
return $im->resize($width, $height, function ($constraint) use($preventUpsize) {
41+
$constraint->aspectRatio();
42+
if($preventUpsize) $constraint->upsize();
43+
});
44+
};
45+
return $this;
46+
}
47+
48+
/** @return ImageHelper */
49+
public function grayscale() {
50+
$this->path .= '_grayscale';
51+
$this->operations[] = function($im) {
52+
return $im->greyscale();
53+
};
54+
return $this;
55+
}
56+
57+
/** @return ImageHelper */
58+
public function multiply($r,$g,$b){
59+
$this->path .= '_multiply'.$r.'_'.$g.'_'.$b;
60+
$this->operations[] = function($im) use($r,$g,$b) {
61+
$srcPath = 'cache/tmp/'.uniqid().'.'.$this->extension;
62+
$im->save($this->pathToDir($srcPath));
63+
64+
switch($this->extension) {
65+
case 'gif':
66+
$src = imagecreatefromgif($this->pathToDir($srcPath));
67+
break;
68+
case 'png':
69+
$src = imagecreatefrompng($this->pathToDir($srcPath));
70+
break;
71+
case 'jpg':
72+
case 'jpeg':
73+
$src = imagecreatefromjpeg($this->pathToDir($srcPath));
74+
break;
75+
default:
76+
throw new Exception("Uknown extension type: " . $this->extension);
77+
}
78+
79+
$filter_r=$r;
80+
$filter_g=$g;
81+
$filter_b=$b;
82+
$imagex = imagesx($src);
83+
$imagey = imagesy($src);
84+
for ($x = 0; $x <$imagex; ++$x) {
85+
for ($y = 0; $y <$imagey; ++$y) {
86+
$rgb = imagecolorat($src, $x, $y);
87+
$TabColors = imagecolorsforindex ( $src , $rgb );
88+
$color_r = floor($TabColors['red']*$filter_r/255);
89+
$color_g = floor($TabColors['green']*$filter_g/255);
90+
$color_b = floor($TabColors['blue']*$filter_b/255);
91+
$newcol = imagecolorallocate($src, $color_r,$color_g,$color_b);
92+
imagesetpixel($src, $x, $y, $newcol);
93+
}
94+
}
95+
return Image::make($src);
96+
};
97+
return $this;
98+
}
99+
100+
public function size(){
101+
return null;
102+
}
103+
104+
public function src(){
105+
$finalPath = 'cache/images/' . $this->path . '.' . $this->extension;
106+
$filePath = $this->pathToDir($finalPath);
107+
108+
109+
if(!file_exists($filePath)){
110+
//If not in cache -> apply operations
111+
$this->image = Image::make($this->pathToDir($this->originalPath));
112+
foreach($this->operations as $operation){
113+
$this->image = $operation($this->image);
114+
}
115+
$this->image->save($filePath);
116+
}
117+
118+
return '/' . $finalPath;
119+
}
120+
121+
private function pathToDir($path){
122+
return getcwd() . '/' . $path;
123+
}
124+
125+
public function __toString(){
126+
return $this->src();
127+
}
128+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.