Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Php » How to upload and resize image in php with example

How To Upload And Resize Image In Php With Example

How to upload and resize image in php with example

Posted on May 12, 2020December 16, 2022 By XpertPhp 1 Comment on How to upload and resize image in php with example

In this tutorial, We will tell you how to upload and resize image in PHP. upload images are the most important part of the website.

In this example, we will upload the image and create a resize image in PHP. if we will upload a large image on our website then our website runs slow. so we need to resize image width and height. the image resize will help our website improve SEO(search engine optimization).

Sometimes, we need to create thumbnails of images. so, we can easily create a thumbnail using the below example. we can also create thumbnails of images in different formate like png, jpg, jpeg, and gif.

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
<?php
if(isset($_FILES['image'])){
$file_name = $_FILES['image']['name']; // The file name  
$file_temp = $_FILES['image']['tmp_name']; // File in the PHP tmp folder
$getext = explode(".", $file_name); // Split file name into an array using the dot
        $fileExt = end($getext); // Now target the last array element to get the file extension
$wmax = 300;
$hmax = 300;
        $new_file_path ='./image/thumb/';
img_resize($file_temp,$wmax, $hmax, $fileExt,$new_file_path);
 
 
}
function img_resize($target,$w, $h, $ext,$new_file_path) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
   $w = $h * $scale_ratio;
} else {
   $h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
$res=imagegif($tci, $new_file_path.time(). "_thump.gif");
} else if($ext =="png"){
$res=imagepng($tci, $new_file_path.time(). "_thump.png");
} else {
$res=imagejpeg($tci, $new_file_path.time(). "_thump.jpg");
}
}
?>
 
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">        
<input type="file" name="image" />
<input type="submit"/>
</form>  
See also  Drag and Drop Reorder Items with jQuery, PHP & MySQL

Php Tags:Image Height and Width Resize, Image Resizer in Pixel, resize image width and height, Resize Photo

Post navigation

Previous Post: Laravel 7 integrate summernote with example
Next Post: create zip file and download using ZipArchive in php

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com