PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > scaling images
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
scaling images

Réponse
 
LinkBack Outils de la discussion
Vieux 20/09/2007, 13h47   #1 (permalink)
Hulf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut scaling images

I have images of varying sizes. I want them to be scaled prior to upload to
a set size of 300 x 200 px


$imageinfo = getimagesize($_FILES['userfile']['tmp_name']);

$ix=$imageinfo[0];
$iy=$imageinfo[1];



//upload the images script
I
  Réponse avec citation
Vieux 20/09/2007, 14h58   #2 (permalink)
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] scaling images

That's nice.

On 9/20/07, Hulf <ross@blue-fly.co.uk> wrote:
> I have images of varying sizes. I want them to be scaled prior to upload to
> a set size of 300 x 200 px
>
>
> $imageinfo = getimagesize($_FILES['userfile']['tmp_name']);
>
> $ix=$imageinfo[0];
> $iy=$imageinfo[1];
>
>
>
> //upload the images script
> I
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Give a man a fish, he'll eat for a day. Then you'll find out he was
allergic and is hospitalized. See? No good deed goes unpunished....
  Réponse avec citation
Vieux 21/09/2007, 14h04   #3 (permalink)
Aaron Saray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scaling images

On Sep 20, 11:05 am, hp...@xentra.ro (Puiu Hrenciuc) wrote:
> Hulf wrote:
> > Sorry my message was cut off.

>
> > Yes I want to scale to 300 x 200px before I upload the image to my folder.
> > Here is my code so far.

>
> > thanks,

>
> > H.

>
> > $imageinfo = getimagesize($_FILES['userfile']['tmp_name']);

>
> > echo $x=$imageinfo[0];
> > echo $y=$imageinfo[1];

>
> > $newwidth = 300;
> > $newheight = 200;

>
> > // Load
> > $thumb = imagecreatetruecolor($newwidth, $newheight);
> > $source = imagecreatefromjpeg($_FILES['userfile']['tmp_name']);

>
> > // Resize
> > imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width,
> > $height);

>
> > // Output
> > $myimage = imagejpeg($thumb);

>
> > $target_path = "../property_images/$property_id/".basename(
> > $_FILES['userfile']['name']);

>
> > $img_url= $property_id."/".basename( $_FILES['userfile']['name']);

>
> > if(move_uploaded_file($myimage, $target_path)) {
> > /* echo "The file ". basename( $_FILES['userfile']['name']).
> > " has been uploaded";*/
> > } else{
> > echo "There was an error uploading the file, please try again!";
> > }

>
> After resizing the image you will have the newly created JPEG
> data (binary) in the $myimage variable, not a file(name).
>
> So you actually have to do something like:
>
> file_put_contents($target_path,$myimage);
>
> This will put the content in the $myimage variable ( the resized JPEG
> data) into the target path. Note that you should also check if the
> uploaded file is a supported image, so do something like :
>
> if($source = @imagecreatefromjpeg($_FILES['userfile']['tmp_name']))
> {
> // file is a supported image type, resize it and save it}
>
> else
> {
> // unsupported image file or not an image file at all,
> // display some error here
>
> }
>
> Note:
> file_put_contents is a PHP5 only function, use fopen,fwrite,fclose
> for PHP4.


Also, you need to bring in a calculation to resize this with the same
dimensions... otherwise you're going to skew your image. From one of
my classes that resizes images very abstractly, I have this (you'll
have to change the $this references to your image...)

/**
* get the sizes
*/
$oldX = imageSX($this->_image);
$oldY = imageSY($this->_image);


/**
* if old x is bigger than max s or old y is bigger than max y
*/
if ($oldX > $this->_maxWidth || $oldY > $this->_maxHeight ) {

if ($oldX > $oldY) {
$newX = $this->_maxWidth;
$newY = $oldY * ($this->_maxWidth / $oldX);
}


if ($oldX < $oldY) {
$newX = $oldX * ($this->_maxWidth / $oldY);
$newY = $this->_maxHeight;
}


if ($oldX == $oldY) {
//means they're a square
if ($this->_maxWidth < $this->_maxHeight) {
$divide = $this->_maxWidth / $oldX;
}
else {
$divide = $this->_maxHeight / $oldY;
}

$newX = $oldX * $divide;
$newY = $oldY * $divide;

}

}
else {
$newX = $oldX;
$newY = $oldY;
}


Now, when you resize, you should use your $newX and $newY so that your
image isn't skewed.



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h55.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10991 seconds with 11 queries