Jump to content

resize image and paste it to another


freakjon

Recommended Posts

Hello,

 

I use this code to resize images:

 

<?php
function makeresize($dir,$pic,$n,$t){
  @list($width, $height, $type, $attr) = @getimagesize($pic);
  $max_w = 700;
  $max_h = 500;
  $ratio = @min($max_w/$width,$max_h/$height);

  
  if ($ratio < 1){

    $w = @floor($ratio*$width);
    $h = @floor($ratio*$height);

    
    $thumb = @imagecreatetruecolor($w,$h);
    if ($t == 'image/jpeg'){$temp = @imagecreatefromjpeg($pic);}
    elseif ($t == 'image/gif'){$temp = @imagecreatefromgif($pic);}
    elseif ($t == 'image/png'){$temp = @imagecreatefrompng($pic);}
   

  
    @imagecopyresampled($thumb,$temp,0,0,0,0,$w,$h,$width,$height);
    if ($t == 'image/jpeg'){@imagejpeg($thumb,"$dir/".$n, 100);}
    elseif ($t == 'image/gif'){@imagegif($thumb,"$dir/".$n, 100);}
    elseif ($t == 'image/png'){@imagepng($thumb,"$dir/".$n, ;}
  }
}
?>



 

 

the  code that calls the function above is:

 

$tipi_consentiti = array("image/gif","image/jpeg","image/png","image/pjpeg");

    // form data
    $titolo = @addslashes($_POST['titolo']);
    $descrizione = @addslashes($_POST['descrizione']);
    $nome = @addslashes($_FILES['imagefile']['name']);
    $path = $carfoto_user . stripslashes($nome);
    $tipo = @addslashes($_FILES['imagefile']['type']);   

............

  
  if ((@in_array($_FILES['imagefile']['type'], $tipi_consentiti))&& ($_FILES["imagefile"]["size"] < 2200000)){
    // copio il file nella cartella delle immagini
    @copy ($_FILES['imagefile']['tmp_name'], $carfoto_user . $nome);

  $nomenew = $t."_".$nome;

    

@makeresize($carfoto_user,$path,$nomenew,$tipo);
    
  
    
    unlink($carfoto_user.$nome); 

 

 

well.

 

everything works fine, but  now I want to paste the resized image produced

on another image that has a fixed size.

It is necessary for javascript gallery output (if the image is too small).

How can I change the above code to do this.

Can you help me?

 

Thanks in advance..sorry for my english please  :-\

 

 

Link to comment
https://forums.phpfreaks.com/topic/219699-resize-image-and-paste-it-to-another/
Share on other sites

here is mine i use.

 

rezise.php

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

then you just place this code on the uploader

<?php

 

$image=new SimpleImage();
$image->load($filetempname);
$image->resize(1900,1200); //<<----- THIS IS THE WIDTH AND HEIGHT
$image->save($filepath.$filename);
?>

 

 

here is mine i use.

 

rezise.php

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

then you just place this code on the uploader

<?php

 

$image=new SimpleImage();
$image->load($filetempname);
$image->resize(1900,1200); //<<----- THIS IS THE WIDTH AND HEIGHT
$image->save($filepath.$filename);
?>

 

thanks,

but it is not suitable to my needs.

The image produced is not proportional to the original image.

 

 

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.