|
Posted by Arjen on 10/31/06 08:18
Beshoo schreef:
> Hi gaiz :::
> Any one has any methode to resize the image in certain width and the
> height will be applied automatically But with out any destoration to
> the image ???
>
> coz i have one, but , it cause destoration to the image !!!!
>
> thanx in advance
>
A couple ..one modified from phpclasses wich relies on imagemagic and
the other modified from a dutch site somewhere wich relies on GD. U need
the latest version of ImageMagic or you will get frequent errors with
the last script. The first script has the drawback that it will generate
black images if something goed wrong.
ps please ignore top posting idiots !
<?php
class foto
{
public $doelbreedte,$doelhoogte,$image,$newimage,$destination;
function convert()
{
if($_FILES['foto']['name'])
{
$bronvanhetbestand = $_FILES['foto']['tmp_name'];
//De hoogte en breedte ophalen van het plaatje
$dimensions = getimagesize($bronvanhetbestand);
//Hoogte en breedte toekennnen aan nieuwe variabelen
$bronbreedte = $dimensions[0];
$bronhoogte = $dimensions[1];
if ($bronbreedte>$bronhoogte)
{
$this->doelhoogte = ($bronhoogte * $this->doelbreedte) / $bronbreedte;
}
else
{
//De nieuwe hoogte berekenen aan de gegevens van het oude plaatje en
de doel breedte
$this->doelbreedte = ($bronbreedte * $this->doelhoogte) / $bronhoogte;
}
//De hoogte, als het nodig is, afronden
$this->doelhoogte = round($this->doelhoogte, 0);
$this->doelbreedte = round($this->doelbreedte, 0);
//Het plaatje inlezen in de variabele $image
$this->image = imagecreatefromjpeg($bronvanhetbestand);
//een nieuw klein plaatje maken met de gewenste grootte
$this->newimage = imagecreatetruecolor($this->doelbreedte,
$this->doelhoogte);
//Het nieuwe plaatje vullen met verkleinde plaatje
imagecopyresampled($this->newimage, $this->image, 0, 0, 0, 0,
$this->doelbreedte, $this->doelhoogte, $bronbreedte, $bronhoogte);
$lala=imagejpeg($this->newimage,$this->destination,100);
}
}
function setsize($size)
{
$this->doelbreedte=$size;
$this->doelhoogte=$size;
}
function setdestination($dest)
{
$this->destination= '/var/www/vhosts/xxx/'.$dest;
}
}
?>
<?
/*
My Usage
@unlink('/var/www/foto/foto/'.$fid.'.jpg');
$imObj = new ImageMagick($_FILES['foto']);
$imObj -> setTargetdir('/var/www/vhosts/xxx');
//. pic
$imObj -> Resize('250','250', 'keep_aspect');
$imObj -> Convert('jpg');
$filename = $imObj -> Save($fid.".jpg");
//thumb
$imObj -> Resize('100','100', 'keep_aspect');
$imObj -> Convert('jpg');
$filename = $imObj -> Save("small".$fid.".jpg");
$imObj -> CleanUp();*/
/*
ImageMagick class 1.0
written by: daniel@bokko.nl
(c)1999 - 2003 All copyrights by: Dani�l Eiland
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
(http://www.gnu.org/licenses/lgpl.txt)
*/
class ImageMagick {
var $targetdir = '';
var $imagemagickdir = '/usr/local/bin';
var $temp_dir = '/var/www/vhosts/xxx'; // httpd must be able to write
there
var $file_history = array();
var $temp_file = '';
var $jpg_quality = '85';
var $count = 0;
var $image_data = array();
var $error = '';
var $verbose = FALSE;
/*
Constructor places uploaded file in $this->temp_dir
Gets the imagedata and stores it in $this->image_data
$filedata = $_FILES['file1']
*/
function ImageMagick($filedata) {
$this->temp_file = time().ereg_replace("[^a-zA-Z0-9_.]", '_',
$filedata['name']);
if(!@rename($filedata['tmp_name'],
$this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file))
die("Imagemagick: Upload failed");
$this->file_history[] =
$this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file;
chmod($this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file,0777);
$this->GetSize();
}
/*
setTargetdir(string string)
Sets the dir to where the images are saved
httpd user must have write access there
*/
function setTargetdir($target) {
if($target == '')
$this->targetdir = $this->temp_dir;
else
$this->targetdir = $target;
if($this->verbose == TRUE) {
echo "Set target dir to '{$this->targetdir}'\n";
}
}
/*
string getFilename()
Returns the current filename
*/
function getFilename() {
return $this->temp_file;
}
/*
array GetSize()
returns the size of the image in an array
array[0] = x-size
array[1] = y-size
*/
function GetSize() {
$command = $this->imagemagickdir."/identify -verbose
'".$this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file."'";
echo $command;
exec($command, $returnarray, $returnvalue);
if($returnvalue)
die("ImageMagick: Corrupt image");
else {
$num = count($returnarray);
for($i=0;$i<$num;$i++)
$returnarray[$i] = trim($returnarray[$i]);
$this->image_data = $returnarray;
}
$num = count($this->image_data);
for($i=0;$i<$num;$i++)
if(eregi('Geometry', $this->image_data[$i])) {
$tmp1 = explode(' ', $this->image_data[$i]);
$tmp2 = explode('x', $tmp1[1]);
$this->size = $tmp2;
return $tmp2;
}
}
/*
Resize(int value, int value, string string)
Resize the image to given size
possible values:
arg1 > x-size, unsigned int
arg2 > y-size, unsigned int
arg3 > resize method;
'keep_aspect' > changes only width or height of image
'fit' > fit image to given size
*/
function Resize($x_size, $y_size, $how='keep_aspect') {
if($this->verbose == TRUE) {
echo "Resize:\n";
}
$method = $how=='keep_aspect'?'>':($how=='fit'?'!':'');
if($this->verbose == TRUE) {
" Resize method: {$how}\n";
}
$command = "{$this->imagemagickdir}/convert -geometry
'{$x_size}x{$y_size}{$method}'
'{$this->temp_dir}/tmp{$this->count}_{$this->temp_file}'
'{$this->temp_dir}/tmp".++$this->count."_{$this->temp_file}'";
if($this->verbose == TRUE) {
echo " Command: {$command}\n";
}
exec($command, $returnarray, $returnvalue);
if($returnvalue) {
$this->error .= "ImageMagick: Resize failed\n";
if($this->verbose == TRUE) {
echo "Resize failed\n";
}
} else {
$this->file_history[] =
$this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file;
}
}
/*
Square(string string)
Makes the image a square
possible arguments:
'center' > crops to a square in the center of the image
'left' > crops to a square on the left side of the image
'right' > crops to a square on the right side of the image
*/
/*
Convert(string string)
Converts the image to any format using the given file extension
Defaults to jpg
*/
function Convert($format='jpg') {
if($this->verbose == TRUE) {
echo "Convert:\n";
}
$name = explode('.' , $this->temp_file);
$name = "{$name[0]}.{$format}";
if($this->verbose == TRUE) {
echo " Desired format: {$format}\n";
echo " Constructed filename: {$name}\n";
}
$command = "{$this->imagemagickdir}/convert -colorspace RGB -quality
{$this->jpg_quality}
'{$this->temp_dir}/tmp{$this->count}_{$this->temp_file}'
'{$this->temp_dir}/tmp".++$this->count."_{$name}'";
if($this->verbose == TRUE) {
echo " Command: {$command}\n";
}
exec($command, $returnarray, $returnvalue);
$this->temp_file = $name;
if($returnvalue) {
$this->error .= "ImageMagick: Convert failed\n";
if($this->verbose == TRUE) {
echo "Convert failed\n";
}
} else {
$this->file_history[] =
$this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file;
}
}
/*
string string Save(string string)
Saves the image to the targetdir, returning the filename
arg1 > set prefix, for example : 'thumb_'
*/
function Save($prefix='') {
if($this->verbose == TRUE) {
echo "Save:\n";
}
if(!@copy($this->temp_dir.'/tmp'.$this->count.'_'.$this->temp_file,
$this->targetdir.'/'.$prefix)) {
$this->error .= "ImageMagick: Couldn't save to
{$this->targetdir}/'{$prefix}{$this->temp_file}\n";
if($this->verbose == TRUE) {
echo "Save failed to {$this->targetdir}/{$prefix}}\n";
}
} else {
if($this->verbose == TRUE) {
echo " Saved to {$this->targetdir}/{$prefix}\n";
}
}
return $prefix;
}
/*
Cleans up all the temp data in $this->tempdir
*/
function Cleanup() {
if($this->verbose == TRUE) {
echo "Cleanup:\n";
}
$num = count($this->file_history);
for($i=0;$i<$num;$i++) {
if(!@unlink($this->file_history[$i])) {
$this->error .= "ImageMagick: Removal of temporary file
'{$this->file_history[$i]}' failed\n";
if($this->verbose == TRUE) {
echo " Removal of temporary file '{$this->file_history[$i]}'
failed\n";
}
} else {
if($this->verbose == TRUE) {
echo " Deleted temp file: {$this->file_history[$i]}\n";
}
}
}
if($this->verbose == TRUE) {
echo '</pre>';
}
}
}
?>
Navigation:
[Reply to this message]
|