Witam.
Posiadam funkcje odpowiedzialne za wgranie i resize obrazka w locie.
Problem zaczyna się, gdy chcę skalować różnej szerokości obrazki.
Tutaj mam wklepany rozmiar dla zdjęć pionowych. Jak zrobić, aby w jakiś sposób wykrywać czy zdjęcie jest poziome i pionowe i wtedy dopasować konkretną proporcję rozmiaru?
Wszystko fajnie działa, gdy obrazek jest pionowy. Przy poziomym zaczynają się schody.
Jak to rozwiązać?
Poniżej fragment kodu
functions.php:
<?php
function createFolder($path)
{
mkdir($path, 0755
, TRUE); }
}
function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
$source = imagecreatefromjpeg($sourcePath);
$width = imagesx($source);
$height = imagesy($source);
$tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
if (imagejpeg($tnumbImage, $targetPath, 90)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
} else {
return FALSE;
}
}
?>
image_upload_submit.php:
<?php
require_once('functions.php');
if(isset($_FILES['image_file_input'])) {
$output['status']=FALSE;
$allowedImageType = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png" );
if ($_FILES['image_file_input']["error"] > 0) {
$output['error']= "File Error";
}
elseif (!in_array($_FILES['image_file_input']["type"], $allowedImageType)) { $output['error']= "Invalid image format";
}
elseif (round($_FILES['image_file_input']["size"] / 1024
) > 4096
) { $output['error']= "Maximum file upload size is exceeded";
} else {
$temp_path = $_FILES['image_file_input']['tmp_name'];
$file = pathinfo($_FILES['image_file_input']['name']); $fileType = $file["extension"];
$fileName = rand(222
, 888
) . time() . ".$fileType";
$small_thumbnail_path = "uploads/small/";
createFolder($small_thumbnail_path);
$small_thumbnail = $small_thumbnail_path . $fileName;
$medium_thumbnail_path = "uploads/medium/";
createFolder($medium_thumbnail_path);
$medium_thumbnail = $medium_thumbnail_path . $fileName;
$large_thumbnail_path = "uploads/large/";
createFolder($large_thumbnail_path);
$large_thumbnail = $large_thumbnail_path . $fileName;
$thumb1 = createThumbnail ($temp_path, $small_thumbnail,$fileType, 160, 193);
$thumb2 = createThumbnail($temp_path, $medium_thumbnail, $fileType, 360, 547);
$thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 460, 647);
if($thumb1 && $thumb2 && $thumb3) {
$output['status']=TRUE;
$output['small']= $small_thumbnail;
$output['medium']= $medium_thumbnail;
$output['large']= $large_thumbnail;
}
}
echo json_encode
($output); }
?>