函数未创建图像但它传递了图像
好吧,问题似乎出在函数中没有创建图像,或者在脚本本身中,它没有将图像传递给函数
这是上传脚本(注意:这是注入到 AJAX 中的)
$path = "cache/";
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
ist($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2024*2024))
{
$new_file = "header.".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$new_file))
{
echo "<img src='cache/".$new_file."?".time()."' class='preview'>";
createHeader($new_file);
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
这是函数
function createHeader($new_file) {
$path = "uploads/";
$image = "cache/";
$width = 800;
if(preg_match('/[.](jpg)|(jpeg)$/', $new_file)) {
$im = imagecreatefromjpeg($image . $new_file);
} else if (preg_match('/[.](gif)$/', $new_file)) {
$im = imagecreatefromgif($image . $new_file);
imagecolortransparent($im, black);
} else if (preg_match('/[.](png)$/', $new_file)) {
$im = imagecreatefrompng($image . $new_file);
}
imagealphablending($im, false);
imagesavealpha($im, true);
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $width;
$ny = floor($oy * ($width / $ox) );
imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
imagealphablending($im, true);
if(!file_exists($path)) {
if(!mkdir($path)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path . $new_file, 100);
imagedestroy($im);
imagedestroy($nm);
}
现在 从逻辑上讲,代码必须执行的操作是,如果使用我的 cms 的用户登录,他或她可以通过单击图标更改页面上的标题,该图标将对 edit.php 进行 jQuery 调用,传递所需的查询字符串。从那里,自己的用户可以上传一个新图像,该图像将被重命名为 header.jpg 并覆盖原始图像
header.jpg 必须上传到 uploads/ 目录,并且原始图像缓存/的副本应该是函数可以取消链接的副本创建一个副本并将其放入 uploads/ 目录中。
发生的情况是,第一个脚本正在上传文件并将其放入缓存/目录中,但该功能似乎不起作用,并且对 uploads/ 目录没有任何作用。
知道我可能做错了什么或没有做导致这个恼人的问题吗?
Ok so the problem seems to be in the function not creating the image, or in the script it self which does not pass the image to the function
Here is the upload script (note: this is injected into AJAX)
$path = "cache/";
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
ist($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2024*2024))
{
$new_file = "header.".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$new_file))
{
echo "<img src='cache/".$new_file."?".time()."' class='preview'>";
createHeader($new_file);
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
now here is the function
function createHeader($new_file) {
$path = "uploads/";
$image = "cache/";
$width = 800;
if(preg_match('/[.](jpg)|(jpeg)$/', $new_file)) {
$im = imagecreatefromjpeg($image . $new_file);
} else if (preg_match('/[.](gif)$/', $new_file)) {
$im = imagecreatefromgif($image . $new_file);
imagecolortransparent($im, black);
} else if (preg_match('/[.](png)$/', $new_file)) {
$im = imagecreatefrompng($image . $new_file);
}
imagealphablending($im, false);
imagesavealpha($im, true);
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $width;
$ny = floor($oy * ($width / $ox) );
imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
imagealphablending($im, true);
if(!file_exists($path)) {
if(!mkdir($path)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path . $new_file, 100);
imagedestroy($im);
imagedestroy($nm);
}
In logic what the code must do is if a user using my cms is loged-in he or she can change the header on the page by clicking an icon which will make a jQuery call to edit.php passing needed query string. From there own user can upload a new image which will be renamed header.jpg and over-write the original image
The header.jpg has to be uploaded into uploads/ directory and a copy of original image cache/ should be unlinked ones the function can create a copy and put it into uploads/ directory.
What is happening is that the first script is uploading the file and is putting it into cache/ directory but the function does not seem to work and does nothing for uploads/ directory.
Any idea what I may be doing wrong or not doing that is causing this annoying problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$path = "缓存/";
$path = "cache/";