-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize-images.php
62 lines (42 loc) · 1.96 KB
/
resize-images.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
59
60
61
62
<?php
use Alura\Threads\Student\InMemoryStudentRepository;
require 'vendor/autoload.php';
use parallel\Runtime;
$repository = new InMemoryStudentRepository();
$studentList = $repository->all();
$studentChunks = array_chunk($studentList, ceil(count($studentList) / 4));
$runTimes = [];
foreach($studentChunks as $key => $studentChunk) {
$runTimes[$key] = new Runtime(__DIR__ . '/vendor/autoload.php');
$runTimes[$key]->run(function (array $students) {
foreach($students as $student) {
echo 'Resizing ' . $student->fullName() . ' profile picture' . PHP_EOL;
$profilePicturePath = $student->profilePicturePath();
[$width, $height] = getimagesize($profilePicturePath);
$ratio = $height / $width;
$newWidth = 200;
$newHeight = 200 * $ratio;
$sourceImage = imagecreatefromjpeg($profilePicturePath);
$destinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($destinationImage, __DIR__ . '/storage/resized/' . basename($profilePicturePath));
echo 'Finished resizing '. $student->fullName() . ' profile picture' . PHP_EOL;
}
}, [$studentChunk]);
//resizeTo200PixelsWidth($student->profilePicturePath());
}
foreach($runTimes as $runTime) {
$runTime->close();
}
echo 'Finalizando Thread principal' . PHP_EOL;
function resizeTo200PixelsWidth($imagePath)
{
[$width, $height] = getimagesize($imagePath);
$ratio = $height / $width;
$newWidth = 200;
$newHeight = 200 * $ratio;
$sourceImage = imagecreatefromjpeg($imagePath);
$destinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($destinationImage, __DIR__ . '/storage/resized/' . basename($imagePath));
}