与我这样的数百万兄弟分享知识并不是一个问题。我几个小时以来一直在使用PHP和jquery旋转图像并动态替换图像。这个例子将说明你更多。
此示例基于Cakephp 2.5。所以不要恐慌。我的Dom看起来像
<div class="review-col campaign_image_upload_section">
<?php
echo $this->Campaign->image($campaign, 200, 200, true);
?>
</div>
在Html中,它返回
<img alt="" src="http://192.168.100.96/XXX/XXX/XXX/files/timthumb.php?src=http://192.168.100.96/XXX/XXX/XXX/app/webroot/files/campaign/image/14/lost_home.jpg&q=95&w=200">
我在旋转链接上绑定了一个jQuery事件。您可以使用带有roate_right类名的简单html链接。事件绑定在&#34; rotate_right&#34;类。我的AJAX请求看起来像
$(".rotate_right").click(function (event) {
event.preventDefault();
$(".campaign_image_upload_section").html(loading());
$.ajax({
url: $(this).attr('href'),
type: "POST",
dataType: 'json',
success: function (res) {
if (res.status) {
//You only could replace image src too
$(".campaign_image_upload_section").html("<img src='"+decodeURIComponent(res.src)+"' width=200 height=200>");
}
else {
alert("error in rotating");
}
}
});
return false;
})
在jQuery的加载功能中,我只向特定div显示加载图像。 我调用的PHP函数看起来像
public function rotate_image_right() {
// File and rotation
//You could use $_GET or pass parameters to this function
$filename = $this->params->query('image');
//PHP's imagerotate function will rotate counter clock wise. If you give 90 degree it will rotate left. use 270 to rotate on time
$degrees = 270;
// Content type - (uncomment below line to show out put in the browser)
// header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
//I am fetching my campaign because to set directory path.
$campaign = $this->Campaign->getData($this->params->query('id'));
// Output
//Set current file name
$cur_file = $this->params->query('image_name');
$path_info = pathinfo($cur_file);
strtolower($path_info['extension']);
try{
if ($path_info['extension'] == 'jpg' || $path_info['extension'] == 'jpeg') {
$function = 'imagejpeg';
} else {
$function = 'image' . $path_info['extension'];
}
{catch (Exception $e) {
// Handle invalid file
}
//My output directory - You change according to your uses
$output_dir = WWW_ROOT . "files/campaign/image/" . $campaign["Campaign"]["id"] . "/";
$function($rotate, $output_dir . $cur_file);
imagedestroy($rotate);
$response["status"] = true;
$response["src"] = urlencode(Router::url("/", true) . 'files/campaign/image/' . $campaign["Campaign"]["id"] . '/' . $cur_file);
//We must encode first to send back data to AJAX
echo json_encode($response);
exit;
}
最后,我对此代码的调用在
之下<?php echo $this->Html->link("Rotate", array("controller" => "campaigns", "action" => "rotate_image_right?image=" . WWW_ROOT . "files/campaign/image/" . $campaign["Campaign"]["id"] . "/" . $campaign["Campaign"]["image"] . "&id=" . $campaign["Campaign"]["id"] . "&image_name=" . $campaign["Campaign"]["image"]), array("class" => "rotate_right")); ?>
答案 0 :(得分:0)
只是一个猜测(那里有很多代码,一般我们喜欢它,如果askers可以将问题代码隔离到+ - 10行),但我认为你缺少了
// Output
imagejpeg($rotate);
正如PHP docs for imagerotate
中所建议的那样。
docs on imagejpeg
解释该函数的工作原理(我相信你会将文件名作为第二个参数传递)。