0

I'm working on script which resizes a picture uploaded with PHP twice.

I'm able to do one resize, but I would like to do another one.

Here is my php code. (//600 is for the second resize).

//Redimensionons l'image
$source = imagecreatefromjpeg($_FILES['new_up']['tmp_name']); // La photo est la source
                                    
// Les fonctions imagesx et imagesy renvoient la largeur et la hauteur d'une image
$largeur_source = imagesx($source);
$hauteur_source = imagesy($source);
$largeur_destination = 460;
//Regle de trois pour calculer la hauteur;
$hauteur_destination = ($hauteur_source * $largeur_destination) / $largeur_source;
                                    
//600
$largeur_destination_600 = 600;
$hauteur_destination_600 = ($hauteur_source * $largeur_destination_600) / $largeur_source;
                                    
// On crée la miniature vide
$destination = imagecreatetruecolor($largeur_destination, $hauteur_destination);
                                    
//600
$destination_600 = imagecreatetruecolor($largeur_destination_600, $hauteur_destination_600);
                                    
// On crée la miniature
imagecopyresampled($destination, $source, 0, 0, 0, 0, $largeur_destination, $hauteur_destination, $largeur_source, $hauteur_source);
                                    
//600
imagecopyresampled($destination_600, $source, 0, 0, 0, 0, $largeur_destination, $hauteur_destination, $largeur_source, $hauteur_source);

// On edit le tmp_name avec les dimensions miniature
imagejpeg($destination,$_FILES['new_up']['tmp_name'] );

//600
imagejpeg($destination_600,$_FILES['new_up']['tmp_name'] );

//Re-name en md5
$filename  = basename($_FILES['new_up']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new       = md5($filename).'.'.$extension;

//Enregistrons l'image
move_uploaded_file($_FILES['new_up']['tmp_name'], 'uploads/' . $new);
echo "L'envoi a bien été effectué !";

//600
move_uploaded_file($_FILES['new_up']['tmp_name'], 'uploads/600/' . $new);
echo "L'envoi a bien été effectué !";
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
casusbelli
  • 463
  • 1
  • 5
  • 22

1 Answers1

0

Without much checking your code (so, there may be other issues), I noticed that your order of commands is wrong: you save the first image, then the second (both to the same file!) and then move the file twice.

It should be done like this:

//Re-name en md5
$filename  = basename($_FILES['new_up']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new       = md5($filename).'.'.$extension;// On edit le tmp_name avec les dimensions miniature

imagejpeg($destination, 'uploads/' . $new);

//600
imagejpeg($destination_600, 'uploads/600/' . $new);

So, just save under the new filenames; don't overwrite the uploaded file (PHP will remove it as soon as the script is done).

Vedran Šego
  • 3,553
  • 3
  • 27
  • 40