Jump to content

image resize


andy_b_1502

Recommended Posts

Hi,

 

I'm having to look hard for the right way to do this.

 

Basically the image is sent to the database, and is called to be put in a table, but the images are all different sizes, i would like them all one size, whats the best way to do this with my existing script:

 

<?php 
error_reporting(E_ALL);
ini_set("display_errors", 1);
echo '<pre>' . print_r($_FILES, true) . '</pre>';


//This is the directory where images will be saved 
$target = "/home/users/web/b109/ipg.removalspacecom/images/COMPANIES"; 
$target = $target . basename( $_FILES['upload']['name']); 

//This gets all the other information from the form 
$company_name=$_POST['company_name']; 
$basicpackage_description=$_POST['basicpackage_description']; 
$location=$_POST['location']; 
$postcode=$_POST['postcode'];
$upload=($_FILES['upload']['name']); 

// Connects to your Database 
mysql_connect("***", "***", "***") or die(mysql_error()) ; 
mysql_select_db("***") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `Companies` (company_name, basicpackage_description, location, postcode,  upload) VALUES ('$company_name', '$basicpackage_description', '$location', '$postcode',  '$upload')") ; 
echo mysql_error();

//Writes the photo to the server 
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['upload']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

Link to comment
https://forums.phpfreaks.com/topic/257181-image-resize/
Share on other sites

i've found this script that is meant to do the job, i dont claim to fully understand it = but by looking at it, it scales and resizes most images BUT it obviously needs an image to actually resize. Would i be able to just put it underneath the existing script i previously uploaded or would there be some messing around to make things fit better for the resize function to work with my script i already have?

 

<?php

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;
   }      

}
?>

Link to comment
https://forums.phpfreaks.com/topic/257181-image-resize/#findComment-1318470
Share on other sites

plus i forgot to add in that it needs a php(include) which is this:

 

<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resize(250,400);
   $image->save('picture2.jpg');
?>

 

where does that go in my orginal script to resize the image at the time of upload. orginal script is this:

 

<?php 
error_reporting(E_ALL);
ini_set("display_errors", 1);
echo '<pre>' . print_r($_FILES, true) . '</pre>';


//This is the directory where images will be saved 
$target = "/home/users/web/b109/ipg.removalspacecom/images/COMPANIES"; 
$target = $target . basename( $_FILES['upload']['name']); 

//This gets all the other information from the form 
$company_name=$_POST['company_name']; 
$basicpackage_description=$_POST['basicpackage_description']; 
$location=$_POST['location']; 
$postcode=$_POST['postcode'];
$upload=($_FILES['upload']['name']); 

// Connects to your Database 
mysql_connect("***", "***", "***") or die(mysql_error()) ; 
mysql_select_db("***") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `Companies` (company_name, basicpackage_description, location, postcode,  upload) VALUES ('$company_name', '$basicpackage_description', '$location', '$postcode',  '$upload')") ; 
echo mysql_error();

//Writes the photo to the server 
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['upload']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

Link to comment
https://forums.phpfreaks.com/topic/257181-image-resize/#findComment-1318471
Share on other sites

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.