1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WrkLst\DocxMustache; |
4
|
|
|
|
5
|
|
|
class DocImage |
6
|
|
|
{ |
7
|
|
|
public function AllowedContentTypeImages() |
8
|
|
|
{ |
9
|
|
|
return [ |
10
|
|
|
'image/gif' => '.gif', |
11
|
|
|
'image/jpeg' => '.jpeg', |
12
|
|
|
'image/png' => '.png', |
13
|
|
|
'image/bmp' => '.bmp', |
14
|
|
|
]; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function GetImageFromUrl($url, $manipulation) |
18
|
|
|
{ |
19
|
|
|
$allowed_imgs = $this->AllowedContentTypeImages(); |
20
|
|
|
|
21
|
|
|
if (trim($url)) { |
22
|
|
|
if ($img_file_handle = @fopen($url.$manipulation, 'rb')) { |
23
|
|
|
$img_data = stream_get_contents($img_file_handle); |
24
|
|
|
fclose($img_file_handle); |
25
|
|
|
$fi = new \finfo(FILEINFO_MIME); |
26
|
|
|
|
27
|
|
|
$image_mime = strstr($fi->buffer($img_data), ';', true); |
28
|
|
|
//dd($image_mime); |
29
|
|
|
if (isset($allowed_imgs[$image_mime])) { |
30
|
|
|
return [ |
31
|
|
|
'data' => $img_data, |
32
|
|
|
'mime' => $image_mime, |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function ResampleImage($parent, $imgs, $k, $data, $dpi = 72) |
42
|
|
|
{ |
43
|
|
|
\Storage::disk($parent->storageDisk)->put($parent->local_path.'word/media/'.$imgs[$k]['img_file_src'], $data); |
44
|
|
|
|
45
|
|
|
//rework img to new size and jpg format |
46
|
|
|
$img_rework = \Image::make($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_src'])); |
47
|
|
|
if($dpi!=72) $img_rework2 = \Image::make($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_src'])); |
48
|
|
|
|
49
|
|
|
$imgWidth = $img_rework->width(); |
50
|
|
|
$imgHeight = $img_rework->height(); |
51
|
|
|
|
52
|
|
|
//check https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/ |
53
|
|
|
// for EMUs calculation |
54
|
|
|
/* |
55
|
|
|
295px @72 dpi = 1530350 EMUs = Multiplier for 72dpi pixels 5187.627118644067797 |
56
|
|
|
413px @72 dpi = 2142490 EMUs = Multiplier for 72dpi pixels 5187.627118644067797 |
57
|
|
|
*/ |
58
|
|
|
$availableWidth = (int) ($imgs[$k]['cx'] / 5187.627118644067797); |
59
|
|
|
$availableHeight = (int) ($imgs[$k]['cy'] / 5187.627118644067797); |
60
|
|
|
|
61
|
|
|
//height based resize |
62
|
|
|
$h = (($imgHeight / $imgWidth) * $availableWidth); |
63
|
|
|
$w = (($imgWidth / $imgHeight) * $h); |
64
|
|
|
|
65
|
|
|
//if height based resize has too large width, do width based resize |
66
|
|
|
if ($h > $availableHeight) { |
67
|
|
|
$w = (($imgWidth / $imgHeight) * $availableHeight); |
68
|
|
|
$h = (($imgHeight / $imgWidth) * $w); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$h = null; |
72
|
|
|
|
73
|
|
|
//for getting non high dpi measurements, as the document is on 72 dpi. |
74
|
|
|
//TODO: this should be improved. it does not really need the resampling to identify the new sizes. |
75
|
|
|
//instead this should just be calculated, as resampling the image is too process instensive. |
76
|
|
|
$img_rework->resize($w, $h, function ($constraint) { |
77
|
|
|
$constraint->aspectRatio(); |
78
|
|
|
$constraint->upsize(); |
79
|
|
|
}); |
80
|
|
|
$new_height = $img_rework->height(); |
81
|
|
|
$new_width = $img_rework->width(); |
82
|
|
|
|
83
|
|
|
if($dpi!=72) |
84
|
|
|
{ |
85
|
|
|
//for storing the image in high dpi, so it has good quality on high dpi screens |
86
|
|
|
$img_rework2->resize(((int)$w*($dpi/72)), $h, function ($constraint) { // make high dpi version for actual storage |
|
|
|
|
87
|
|
|
$constraint->aspectRatio(); |
88
|
|
|
$constraint->upsize(); |
89
|
|
|
}); |
90
|
|
|
$img_rework2->save($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest'])); |
91
|
|
|
|
92
|
|
|
//set dpi of image to high dpi |
93
|
|
|
/*$im = new \imagick(); |
|
|
|
|
94
|
|
|
$im->setResolution($dpi,$dpi); |
95
|
|
|
$im->readImage($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest'])); |
96
|
|
|
$im->writeImage($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest'])); |
97
|
|
|
$im->clear(); |
98
|
|
|
$im->destroy(); |
99
|
|
|
*/ |
100
|
|
|
} |
101
|
|
|
else { |
102
|
|
|
$img_rework->save($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest'])); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$parent->zipper->folder('word/media')->add($parent->StoragePath($parent->local_path.'word/media/'.$imgs[$k]['img_file_dest'])); |
106
|
|
|
|
107
|
|
|
return [ |
108
|
|
|
'height' => $new_height, |
109
|
|
|
'width' => $new_width, |
110
|
|
|
'height_emus' => (int) ($new_height * 5187.627118644067797), |
111
|
|
|
'width_emus' => (int) ($new_width * 5187.627118644067797), |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.