Search
 
SCRIPT & CODE EXAMPLE
 

PHP

change aspect ratio of image php

function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir)
{
    $path = $uploadDir . '/' . $image_name;

    $mime = getimagesize($path);

    if($mime['mime']=='image/png') { 
        $src_img = imagecreatefrompng($path);
    }
    if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
        $src_img = imagecreatefromjpeg($path);
    }   

    $old_x          =   imageSX($src_img);
    $old_y          =   imageSY($src_img);

    if($old_x > $old_y) 
    {
        $thumb_w    =   $new_width;
        $thumb_h    =   $old_y*($new_height/$old_x);
    }

    if($old_x < $old_y) 
    {
        $thumb_w    =   $old_x*($new_width/$old_y);
        $thumb_h    =   $new_height;
    }

    if($old_x == $old_y) 
    {
        $thumb_w    =   $new_width;
        $thumb_h    =   $new_height;
    }

    $dst_img        =   ImageCreateTrueColor($thumb_w,$thumb_h);

    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 


    // New save location
    $new_thumb_loc = $moveToDir . $image_name;

    if($mime['mime']=='image/png') {
        $result = imagepng($dst_img,$new_thumb_loc,8);
    }
    if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
        $result = imagejpeg($dst_img,$new_thumb_loc,80);
    }

    imagedestroy($dst_img); 
    imagedestroy($src_img);

    return $result;
}
Comment

PREVIOUS NEXT
Code Example
Php :: php language is used for 
Php :: how to add x-xss-protection header 
Php :: laravel select only one word from string 
Php :: Uncaught jquery-numerator requires jQuery to be loaded first wordpress 
Php :: silverstripe image upload field 
Php :: php remove non printable characters 
Php :: why the laravel project have many cache 
Php :: php encrypt password 
Php :: laravel longblob migration 
Php :: php howto ignore file with BOM 
Php :: how to reverse a string in php 
Php :: conditional validation laravel based on role 
Php :: Laravel 8 Auth Scaffolding using Inertia Jetstream 
Php :: adjacent post sort order by post title 
Php :: php concat variable and string 
Php :: limit wordpress search to title 
Php :: octobercms mail register 
Php :: morph relation laravel 
Php :: "IlluminateDatabaseEloquentMassAssignmentException" 
Php :: Redirect User To Different Page 
Php :: generate report daily weekly monthly php mysql 
Php :: how to add files in child theme in theme editor 
Php :: mssql php 
Php :: php 7 The each() function is deprecated. 
Php :: laravel redis sentinel 
Php :: guzzle download file 
Php :: xdebug phpstorm 
Php :: withcookie function in php 
Php :: laravel show method 
Php :: value() in laravel 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =