PHP изменяет размер изображения пропорционально максимальной ширине или весу

Существует какой-либо скрипт php, который пропорционально масштабирует изображение с max widht или height ??

Пример: я загружаю изображение, и этот оригинальный размер w: 500 ч: 1000. Но, я хочу изменить размер, что максимальная высота – это ширина и высота 500 … Что сценарий изменяет размер изображения для w: 250 ч: 500

Все, что вам нужно, это соотношение сторон. Что-то в этом роде:

$fn = $_FILES['image']['tmp_name']; $size = getimagesize($fn); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = 500; $height = 500/$ratio; } else { $width = 500*$ratio; $height = 500; } $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width,$height); imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); imagedestroy($src); imagepng($dst,$target_filename_here); // adjust format as needed imagedestroy($dst); 

Вам нужно будет добавить некоторую проверку ошибок, но это должно вас начать.

Используйте класс загрузки, написанный Колином Веротом. У него есть все возможности для изменения размера, редактирования, водяных знаков и т. Д. Это потрясающе!

Класс поддерживается и используется веб-сайтами по всему Интернету, поэтому вы можете полагаться на него, чтобы быть надежным!

См. Здесь

Несмотря на то, что это называется классом загрузки, вы можете применить те же методы к файлам, уже находящимся на вашем сервере

Как использовать

Соблюдайте инструкции по установке на сайте, просто загрузите класс и разместите его на своем сайте.

Тогда ваш скрипт будет выглядеть примерно так:

 // Include the upload class include('class.upload.php'); // Initiate the upload object based on the uploaded file field $handle = new upload($_FILES['image_field']); // Only proceed if the file has been uploaded if($handle->uploaded) { // Set the new filename of the uploaded image $handle->file_new_name_body = 'image_resized'; // Make sure the image is resized $handle->image_resize = true; // Set the width of the image $handle->image_x = 100; // Ensure the height of the image is calculated based on ratio $handle->image_ratio_y = true; // Process the image resize and save the uploaded file to the directory $handle->process('/home/user/files/'); // Proceed if image processing completed sucessfully if($handle->processed) { // Your image has been resized and saved echo 'image resized'; // Reset the properties of the upload object $handle->clean(); }else{ // Write the error to the screen echo 'error : ' . $handle->error; } } в // Include the upload class include('class.upload.php'); // Initiate the upload object based on the uploaded file field $handle = new upload($_FILES['image_field']); // Only proceed if the file has been uploaded if($handle->uploaded) { // Set the new filename of the uploaded image $handle->file_new_name_body = 'image_resized'; // Make sure the image is resized $handle->image_resize = true; // Set the width of the image $handle->image_x = 100; // Ensure the height of the image is calculated based on ratio $handle->image_ratio_y = true; // Process the image resize and save the uploaded file to the directory $handle->process('/home/user/files/'); // Proceed if image processing completed sucessfully if($handle->processed) { // Your image has been resized and saved echo 'image resized'; // Reset the properties of the upload object $handle->clean(); }else{ // Write the error to the screen echo 'error : ' . $handle->error; } }