lhj2100
V2EX  ›  PHP

我想用php实现这么个功能..别人写的代码不会改 所以求帮忙

  •  
  •   lhj2100 · Jun 6, 2012 · 5954 views
  •   You need to sign in to view this topic
    This topic created in 5258 days ago, the information mentioned may be changed or developed.
    从一个页面上传一个图片 文件 JPG bmp 图片限制2M以内
    然后用PHP 处理图片 分析图片尺寸 若图片为800*600 则分割成198*198的图片 12个 并显示出来

    每张图片抛弃2px;
    第一张图片对应原图片 左上角0,0,右下角198,198,第2张图片左上角200,0 右下角398,198


    为了实现这个功能我居然连按键精灵都用上了

    那位同学有时间的帮忙一下

    另外求解释一下 PHP代码中 $foo=$foo.$bar 是什么含义
    16 replies  •  1970-01-01 08:00:00 +08:00
    lulu0401
        1
    lulu0401  
       Jun 6, 2012   ❤️ 1
    $foo=$foo.$bar是吧2个string连接起来吧
    lhj2100
        2
    lhj2100  
    OP
       Jun 6, 2012
    @lulu0401
    代码
    $foo="hello ";
    $bar="world!"
    $foo=$foo.$bar;
    echo $foo;

    结果
    网站在检索 http://1.lise.sinaapp.com/index.php 时遇到错误。 该网站可能关闭进行维护或配置不正确

    => 初学者真无敌...
    qiayue
        3
    qiayue  
       Jun 6, 2012   ❤️ 2
    @lhj2100 第二行 $bar="world!" 没有分号
    alsotang
        4
    alsotang  
       Jun 6, 2012   ❤️ 1
    。。。没有分号......
    PHP代码应该先本地测试再放上sinaapp的,sinaapp的报错不准确。
    lhj2100
        5
    lhj2100  
    OP
       Jun 6, 2012
    @qiayue 真无语... 上边的哪个图片的问题您会吗?
    CoX
        6
    CoX  
       Jun 6, 2012   ❤️ 1
    反正用python的PIL非常easy
    ety001
        7
    ety001  
       Jun 6, 2012
    安装GD库,用GD库做,剩下的就是计算了,估计应该可以写成循环,循环12次。
    qiayue
        8
    qiayue  
       Jun 6, 2012   ❤️ 1
    @lhj2100
    这里 http://www.php.net/manual/zh/ref.image.php 能找到你需要使用的函数
    imagecreatefromjpeg 由文件或URL创建一个新图象
    imagecopy 拷贝图像的一部分
    imagejpeg 输出图象到浏览器或文件
    lhj2100
        9
    lhj2100  
    OP
       Jun 7, 2012
    $s = new SaeStorage();
    $imagedomain="upload";
    $name="233.jpg";
    $imgurl=$s->getUrl($imagedomain,$name);//此处$imgurl 得到文件位于SaeStorage的完整URL
    $imagesize = getimagesize($imgurl);
    var_dump($imagesize);

    返回的结果是bool(false)

    @我可以用一种最温柔的想象 让自己 不再忧伤...
    @qiayue
    ElmerZhang
        10
    ElmerZhang  
       Jun 7, 2012   ❤️ 1
    @lhj2100 getimagesize的参数需要是本地文件。

    $tmpfile = tempnam(SAE_TMP_PATH, "IMG");
    $imagedomain="upload";
    $name="233.jpg";
    copy("saestor://$imagedomain/$name", $tmpfile);
    $imagesize = getimagesize($tmpfile);
    var_dump($imagesize);
    lhj2100
        11
    lhj2100  
    OP
       Jun 7, 2012
    @ElmerZhang 谢谢大大...

    saestor://$imagedomain/$name

    原来这样使用SaeStorage啊
    sea官方的文档上面好像没写
    你在哪里知道的
    ElmerZhang
        12
    ElmerZhang  
       Jun 7, 2012   ❤️ 1
    @lhj2100 saestor:// 这种写法是为了方便移植的,虽然代码能省几行,但是性能差。平时还是用 SaeStorage 操作比较好
    文档:http://sae.sina.com.cn/?m=devcenter&catId=218
    lhj2100
        13
    lhj2100  
    OP
       Jun 7, 2012
    list($current_width, $current_height) = getimagesize($tmpfile);
    $left = 0;
    $top = 0;
    $crop_width = 30;
    $crop_height = 30;
    $canvas = imagecreatetruecolor($crop_width, $crop_height);
    $current_image = imagecreatefromjpeg($tmpfile);
    imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
    $newimages=imagejpeg($canvas, $tmpfile, 100);

    echo $canvas;

    imagecopy 处理过之后生成的新图片 应该就是 $canvas
    但 为什么$canvas仍然是一个字符串呢 Resource id #23
    怎么将新图片显示在浏览器中呢
    $newimages居然也是个字符串 1 《==我雷格去 这是要干什么 输出1表示执行成功了么?
    php的语法真实匪夷所思
    lhj2100
        14
    lhj2100  
    OP
       Jun 7, 2012
    弄懂了 新的图片在SAE_TMP_PATH 里头
    而$tmpfile 直接echo 出来的字符串 就是执行完成之后新图片的具体位置
    执行一次$s->upload($imagedomain,"1.jpg",$tmpfile);
    1.jpg就躺在seaStorage里头 ..
    我得意的笑...
    lhj2100
        15
    lhj2100  
    OP
       Jun 8, 2012
    为什么将下边的代码放入循环以后
    for ($i =1; $i<=9;$i++)
    {
    //echo $i;

    $canvas =imagecreatetruecolor($crop_width, $crop_height);
    $current_image =@imagecreatefromjpeg($tmpfile);
    imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
    imagejpeg($canvas, $tmpfile, 85);
    //header('Content-Type: image/jpeg');
    // imagejpeg($canvas, 100);
    //echo $tmpfile;
    $targetfile=$i.".jpg";
    $s->upload("upload",$targetfile,$tmpfile);
    $imgurl=$s->getUrl("upload",$targetfile);
    echo "<img src='".$imgurl."?".time()."'/>";
    imagedestroy($canvas);
    imagedestroy($current_image);
    $left+=1;
    $top+=1;
    //echo $left."<br/>";
    }

    会出现像http://1.lise.sinaapp.com/file_upload.html
    这样的错误...
    产生的图片为什么有黑边呢
    lhj2100
        16
    lhj2100  
    OP
       Jun 8, 2012
    @ElmerZhang
    @qiayue
    召唤一下大大们..
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4886 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    ♥ Do have faith in what you're doing.