Monday, 17 June 2019

Resize Image Function With PHP

Category : PHP Script
Do you have problem with resize image on the fly with php? or why i must resize image with php? you need to resize your image, because if your user give you image with 1000x1000 and you only display it on 100x100 it will waste your bandwidth. So to solve your problem here is function to resize your image on the fly with php

<?
function resize_img($source,$dest,$MaxWidth,$MaxHeight,$Quality) {
         list($ImageWidth,$ImageHeight,$TypeCode)=getimagesize($source);
         $ImageType=($TypeCode==1?"gif": ($TypeCode==2?"jpeg":
                    ($TypeCode==3?"png":FALSE)));
         $CreateFunction="imagecreatefrom".$ImageType;
         $OutputFunction="image".$ImageType;
         if ($ImageType) {
           $Ratio=($ImageHeight/$ImageWidth);
           $ImageSource=$CreateFunction($source);
           if ($ImageWidth > $MaxWidth || $ImageHeight > $MaxHeight) {
             if ($ImageWidth > $MaxWidth) {
                $ResizedWidth=$MaxWidth;
                $ResizedHeight=$ResizedWidth*$Ratio;
             }
             else {
               $ResizedWidth=$ImageWidth;
               $ResizedHeight=$ImageHeight;
             }       
             if ($ResizedHeight > $MaxHeight) {
               $ResizedHeight=$MaxHeight;
               $ResizedWidth=$ResizedHeight/$Ratio;
             }      
             $ResizedImage=imagecreatetruecolor($ResizedWidth,$ResizedHeight);
             imagecopyresampled($ResizedImage,$ImageSource,0,0,0,0,$ResizedWidth,
                                $ResizedHeight,$ImageWidth,$ImageHeight);
           }
           else {
             $ResizedWidth=$ImageWidth;
             $ResizedHeight=$ImageHeight;     
             $ResizedImage=$ImageSource;
           }   
           $OutputFunction($ResizedImage,$dest,$Quality);
           return true;
         }   
         else
           return false;
    }
?>

Just copy and paste above function to your php file. here is some example how it works

<?
 resize_img("/path/to/source/images.gif","/path/to/destination/image.gif",200,200,100);
// above comment will resize your orginal image to 200 x 200
?>

Hope this function can help you

Like this articles? share it with Share With FacebookFacebook
<< Back
0 Comment | 2760 hits | Posted by Callrid at 2011-01-01 06:28:33

Related Read

Add Comment
Name :
Web :
Comment :

Code :
 

Other Source
A Practical Guide On Complete Multivitamins
Many of us have a hard time deciding what the best multivitamin is for us. This is completely confusing and difficult especially when there are so many bottles to scan in the various aisles of the...

High Blood Pressure
The nutritional benefits of low-fat dairy products are well known for ages. The latest studies show that the ingredients of dairy products - some combination of milk proteins are effective and...

Welcome To Mimicwatch.com - Quality Luxury Replica Vacheron Constantin Watches At Affordable Price
When entering our online store at Mimicwatch.com, you are given an unique opportunity to choose among a wide selection of famous Replica Watches and Replica handbags such as Replica Vacheron...

Ebook Marketing Tips
A great way to have a product to sell on the Internet is an e-book which almost anyone can create. There is no longer any need for delaying with the simple process detailed below. If you are afraid...

Tactical Asset Marketing
Asset Management Different Asset Allocation Strategies Asset marketing is the process of dividing a portfolio into major asset categories such as cash, stocks, real estate, or...

Shout Box
2019-04-10 21:32:43 
StivenSortPat
http://newseastwest.com/was-mukesh-ambanis-son-akash-ambani-driving-the-sports-car-that-killed-two-people/comment-page-1/#comment-338201
2019-02-26 22:20:45 
Playavmk

2016-11-24 08:56:05 
Pembicara Internet M
Website yagn bagus
2016-11-24 08:40:45 
Produsen Mukena
Good Website
2016-04-05 20:44:45 
Belanja Alat ukur
Belanja Alat ukur dan Thermal Camera Hanya di dutapersada.co.id

Site Partners


Search Articles

Web Statistic
No Visitor :
3266356
Today Visitor :
264
Online Visitor :
1
what is my ip address? Page Rank Checker

< Jun 2019 >
SMTWTFS
      1
2345678
9101112131415
16171819202122
23242526272829
30

Increase Page Rank

Last Modified 08-November-2011, 23:21:04 pm
callrid.com Created 2006-2012 by Anton Ongsono
All rights reserved