使用PHP无法正确生成图像缩略图

时间:2013-05-31 09:24:37

标签: php file-upload

我在PHP中编写了一个脚本来上传图像。

到目前为止,我的目标是上传并向服务器发送2张图片,1张原件,1张是缩略图。我的脚本有效,但并不完美。 这是我的剧本

<?php

     //this is script for get  data type file

      $acak        = rand(000000,999999);// for random
      $lokasi_file = $_FILES['fupload']['tmp_name'];
      $nama_file   = $_FILES['fupload']['name'];

      $nama_file_acak = $acak.$nama_file;


      $ukuran_file = $_FILES['fupload']['size'];
      $tipe_file   = $_FILES['fupload']['type'];
      $direktori   = "fkendaraan/$nama_file_acak";

       $uplod = move_uploaded_file($lokasi_file,"$direktori"); //to move image from local to the server folder

     //to handle uplod thumbnail image
      $img = imagecreatefromjpeg($direktori);

      $width = imagesx($img);
      $height = imagesy($img);

      $new_width = 200;

      $new_height = ($new_width/$width) * $height;

      $tmp_img = imagecreatetruecolor( $width, $height );

      imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      //imagecopyresized( $tmp_img, $img, 200, 200, 0, 0, $new_width, $new_height, $width, $height );

      imagejpeg( $tmp_img, $direktori."thumb-".$nama_file_acak );

      imagedestroy($tmp_img);
      imagedestroy($direktori);

// --------------------------------------------- ------------------

//I have no Problem with query and database, it works fine
$sql = "";
$query = mysql_query($sql);


?>

这可以运行但不完美,因为结果是这样的 enter image description here

enter image description here

任何人都可以帮我解决这个问题吗?我非常nubie在PHP

1 个答案:

答案 0 :(得分:5)

尝试更改此内容:

$tmp_img = imagecreatetruecolor( $width, $height );

对此:

$tmp_img = imagecreatetruecolor( $new_width, $new_height );

无论如何,我建议你为这些任务使用一些类,例如: Shiege Iseng Resize Class

但是,当然,如果你想用这个来学习,那没关系:)