Jump to content

Resize images in php before uploading?


EchoFool

Recommended Posts

Hey,

 

 

I was wondering if there is a way to use PHP to resize images when they are a above a size limit?

 

 

Im not referring to file size here, but rather the width by height ratio. As currently i upload images and echo them with:

 

 

style="width:250px;height:200px;"

 

But this means my server is loading large images when i would rather resize them down upon upload to reduce loading times...

 

 

Is this possible in php ?

Link to comment
https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/
Share on other sites

HERE ARE MY SCRIPTS

Resize.PHP

 

<?php
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {
   
   var $image;
   var $image_type;

   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}
?>

 

 

 

The Upload.php

 

<?php
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
include "../../connect.php";
include('Resize.php');
$username = mysql_real_escape_string($_GET['username']);


if(	$username)
{
echo $username;	
$root ="../../../users/";
$folder ="/images/";
$folder1 ="pictures/";
$folder2 ="thumb/";
$filepath =$root.$username.$folder.$folder1;
$filepath1 =$root.$username.$folder.$folder1.$folder2;
if(!file_exists($root))
{
mkdir($root,0777);

chmod($root,0777);
}
if(!file_exists($root.$username))
{
mkdir($root.$username,0777);

chmod($root.$username,0777);
}
if(!file_exists($root.$username.$folder))
{
mkdir($root.$username.$folder,0777);

chmod($root.$username.$folder,0777);
}
if(!file_exists($root.$username.$folder.$folder1))
{
mkdir($root.$username.$folder.$folder1,0777);

chmod($root.$username.$folder.$folder1,0777);
}
if(!file_exists($root.$username.$folder.$folder1.$folder2))
{
mkdir($root.$username.$folder.$folder1.$folder2,0777);

chmod($root.$username.$folder.$folder1.$folder2,0777);
}

if(!file_exists($filepath))
{
mkdir($filepath,0777);
chmod($filepath,0777);
}
if(!file_exists($filepath1))
{
mkdir($filepath1,0777);
chmod($filepath1,0777);
}

$filetempname=$_FILES['Filedata']['tmp_name'];
$ext =".wsi";
$filename=md5(md5($filetempname)).$ext;

$folder3 ="_Pictures";
$userfolder2 =$username.$folder3;
//Ok let resize thie image to be small
$image=new SimpleImage();
$image->load($filetempname);
$image->resize(100,100);
$image->save($filepath1.$filename);

$image=new SimpleImage();
$image->load($filetempname);
$image->resize(1900,1000);
$image->save($filepath.$filename);
//SELECT WHERE AND FOLDER IF RETURN 0 INSERT INTO ELSE RUN THE REST NO UPDATE IS REQUIRED.
$get_username_and_folder = "SELECT * FROM user_image_folders WHERE username = '$username' AND user_folder='$userfolder2'";
$username_folder_results = mysql_query($get_username_and_folder) or die("Data not found."); 
//Get the number of results from the query.
$check_username_and_folder = mysql_num_rows($username_folder_results);
if($check_username_and_folder == 0) 
{
$setuserfolder23 = mysql_query("INSERT INTO user_image_folders(username,user_folder) VALUES ('$username','$userfolder2')") or die(mysql_error());	
}

$timeanddate = date("l, F d, Y h:i" ,time());
//$setuseravatar= mysql_query("UPDATE accounts SET background = 'http://wiistream.info/users/$username/images/background/$filename' WHERE username = '$username'");


$setuserfolder3 = mysql_query("INSERT INTO user_photo_gallery (username,user_folder,large_pic,small_pic,pic_title,pic_discription) VALUES ('$username','$userfolder2',
'http://wiistream.info/users/$username/images/pictures/$filename','http://wiistream.info/users/$username/images/pictures/thumb/$filename','$userfolder2','$userfolder2')") or die(mysql_error());	

$setuserfolder = mysql_query("INSERT INTO user_comments (username,comment,timestamp,title,picture,pic_status,pic_url,count) VALUES('$username','','$timeanddate','$username uploaded a new picture on','http://wiistream.info/users/$username/images/pictures/thumb/$filename','1','http://wiistream.info/users/$username/images/pictures/$filename','0')") or die(mysql_error());
//If there is a match for the username, echo the the users xml data.

}
else 
{
echo "WiiStream Eorror!";
}



?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.