douxin9135 2013-06-19 13:36
浏览 73

使用php调整上传图片大小两次

I'm working on script wich resize 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é !";
  • 写回答

1条回答 默认 最新

  • dqjjw04440 2013-06-19 13:46
    关注

    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).

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line