Aceeasi reactie am avut si eu la inceput, ulterior am inteles ca testul asta e un fel de catch all, in ideea ca au mai multe pozitii si in functie de ce teste rezolv si de modul de rezolvare discutam despre unde as fi mai potrivit.
Testul e mai amplu, am avut o saptamana la dispozitie, include o parte de front-end (realizarea unui layout responsive dupa un PSD dat + nav dinamic cu js/jquery), o parte de PHP cu testul din topicul asta plus inca un script care sa interactioneze cu Facebook API (fara sa folosesc cURL) si inca trei intrebari despre detaliat o metodologie / arhitectura (una cu tangente spre MySQL, una spre dev ops si una spre front-end).
Chiar daca si pentru mine tipul asta de “interviu” e un red flag, am zis sa continui pentru experienta, in ultimii 3 ani n-am mai fost la niciun interviu si din pacate e momentul sa caut altceva.
Multumesc tuturor pentru ajutor si sugestii, dupa cum a zis si tekkie, am facut scriptul cu getopt
. Am folosit libraria GD pentru ca e deja integrata in PHP. Pun scriptul mai jos:
#!/usr/bin/env php
<?php
function request(array $options = []) {
$defaults = [
'path' => '',
'width' => '',
'height' => '',
'name' => ''
];
$options += $defaults;
$args = getopt('', ['path:', 'name:', 'width::', 'height::',]) + $options;
$path = $args[path];
$filename = $args[name];
$new_width = (int) $args[width];
$new_height = (int) $args[height];
return resizeImage($path, $filename, $new_width, $new_height);
}
function resizeImage($path, $filename, $new_width, $new_height) {
if (check_path($path)) {
list($width, $height) = getimagesize($path);
}
if ($new_width === 0) {
$new_width = $width;
if ($width < $height && $new_width < $width) {
$new_width = $width / ($height / $new_height);
}
}
if ($new_height === 0) {
$new_height = $height;
if ($width > $height && $new_height < $height) {
$new_height = $height / ($width / $new_width);
}
}
$thumb = imagecreatetruecolor($new_width, $new_height);
$source = imagecreatefromjpeg($path);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if (!empty($filename)) {
imagejpeg($thumb, $filename, 100);
} else {
print "Filename cannot be empty.\n";
exit();
}
return print "New image $filename was created succesfully in $path.\nThe image size is $new_width x $new_height px.\n";
}
function check_path($path) {
if ((file_exists($path)) && (exif_imagetype($path) === IMAGETYPE_JPEG)) {
return true;
} else {
print "Error: not a jpg image or invalid path.\nWorks only with jpg images.\nAvailable options are: --path, --filename, --width and --height.\nPath and filename are required.\n";
exit();
}
}
request();
?>