dragon8997474 2011-11-13 20:16
浏览 15
已采纳

动态水印PHP上传冲突

I have a watermarking script, some of you might have seen before from one of my previous questions:

<?php 

$imagesource = $_GET['path'];

$filetype = substr($imagesource,strlen($imagesource)-4,4);

$filetype = strtolower($filetype);

if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);

if (!$image) die();

$watermark = @imagecreatefrompng('watermark_'.(imagesx($image) <= 1100 ? "port" : "lans").'.png');

$imagewidth = imagesx($image);
$imageheight = imagesy($image);

$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);

$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);

imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth,
$watermarkheight);

imagejpeg($image);

imagedestroy($image);

imagedestroy($watermark);

?> 

The script works fine, but when my client uploads a picture which is over 2000px X 2000px (or smething around there) the script outputs nothing not even an error. But, when I reduce it to 1100px X 800px for example this script outputs and displays the image. Is there any reason for this?

Is there anyway to either diagnose what is causing this and hence find a solution or to automatically re-size the image on upload?

This is the upload script I currently have:

<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "<h3>Invalid file</h3>";
  }
?>
  • 写回答

2条回答 默认 最新

  • doude1917 2011-11-14 21:24
    关注

    Thanks to Josh I have found this direct solution which I have integrated. Thanks again!

    <?php
    if($_REQUEST['post']==1){
    if (($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg"))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
    
    
        if (file_exists("uploads/" . $_FILES["file"]["name"]))
          {
          echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
          }
        else
          {
    
    // Temporary upload image name 
    $original_image = $_FILES['file']['tmp_name']; 
    
    // Get the image dimensions 
    $size=getimagesize( $original_image ); 
    
    // Name to save the image as - in this case the same as the original 
    $new_image = $_FILES['file']['name']; 
    
    // Maximum image width 
    $max_width = "1600"; 
    
    // Maximum image height 
    $max_height = "1600"; 
    
    // Resize the image and save 
    exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image"); 
    
    echo "File uploaded<br><br>"; 
    
    echo "<img src=\"'uploads/".$new_image."\" width=\"900\"><br><br>"; 
    
    
    $copy = copy($new_image, "uploads/".$new_image);
    $delete = unlink($new_image);
    
    if($copy){
          echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
    
          $date = date("d/m/y");
    
    
          $query  = mysql_query ('INSERT INTO  `ap_photos_list` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'")');
    
          $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "6.00" , \'6" x 4"\')');
          $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "8.00" , \'5" x 7"\')');
          $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "10.00" , \'8" x 6"\')');
          $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "12.00" , \'10" x 8"\')');
          $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "15.00" , \'12" x 8"\')');
    }else{
        echo "Unable to copy file";
    }
          }
        }
      }
    else
      {
      echo "<h3>Invalid file</h3>";
      }
    ?>
    
    <br />
    <br />
    <h3>
    <?php
    if($query){
        echo "Successful";
    }else{
        echo "Unsuccessful";
    }
    }
    ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题