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

2012-06-06 17:38:24 +08:00
 lhj2100
从一个页面上传一个图片 文件 JPG bmp 图片限制2M以内
然后用PHP 处理图片 分析图片尺寸 若图片为800*600 则分割成198*198的图片 12个 并显示出来

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


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

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

另外求解释一下 PHP代码中 $foo=$foo.$bar 是什么含义
4605 次点击
所在节点    PHP
16 条回复
lulu0401
2012-06-06 17:51:03 +08:00
$foo=$foo.$bar是吧2个string连接起来吧
lhj2100
2012-06-06 18:18:53 +08:00
@lulu0401
代码
$foo="hello ";
$bar="world!"
$foo=$foo.$bar;
echo $foo;

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

=> 初学者真无敌...
qiayue
2012-06-06 18:31:33 +08:00
@lhj2100 第二行 $bar="world!" 没有分号
alsotang
2012-06-06 18:36:37 +08:00
。。。没有分号......
PHP代码应该先本地测试再放上sinaapp的,sinaapp的报错不准确。
lhj2100
2012-06-06 18:44:05 +08:00
@qiayue 真无语... 上边的哪个图片的问题您会吗?
CoX
2012-06-06 18:47:31 +08:00
反正用python的PIL非常easy
ety001
2012-06-06 19:10:33 +08:00
安装GD库,用GD库做,剩下的就是计算了,估计应该可以写成循环,循环12次。
qiayue
2012-06-06 20:26:51 +08:00
@lhj2100
这里 http://www.php.net/manual/zh/ref.image.php 能找到你需要使用的函数
imagecreatefromjpeg 由文件或URL创建一个新图象
imagecopy 拷贝图像的一部分
imagejpeg 输出图象到浏览器或文件
lhj2100
2012-06-07 11:31:19 +08:00
$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
2012-06-07 12:27:02 +08:00
@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
2012-06-07 12:52:41 +08:00
@ElmerZhang 谢谢大大...

saestor://$imagedomain/$name

原来这样使用SaeStorage啊
sea官方的文档上面好像没写
你在哪里知道的
ElmerZhang
2012-06-07 13:32:54 +08:00
@lhj2100 saestor:// 这种写法是为了方便移植的,虽然代码能省几行,但是性能差。平时还是用 SaeStorage 操作比较好
文档:http://sae.sina.com.cn/?m=devcenter&catId=218
lhj2100
2012-06-07 14:45:02 +08:00
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
2012-06-07 15:06:02 +08:00
弄懂了 新的图片在SAE_TMP_PATH 里头
而$tmpfile 直接echo 出来的字符串 就是执行完成之后新图片的具体位置
执行一次$s->upload($imagedomain,"1.jpg",$tmpfile);
1.jpg就躺在seaStorage里头 ..
我得意的笑...
lhj2100
2012-06-08 23:18:37 +08:00
为什么将下边的代码放入循环以后
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
2012-06-08 23:19:16 +08:00
@ElmerZhang
@qiayue
召唤一下大大们..

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/38590

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX