I created a project with PHP Laravel and Vue JS. and configured it with Amazon AWS's basic plan. In the beginning, it worked well without any issues. But now, when I try to add a new blog post or edit already existed blog post with heavy content, it is showing 503 Service Unavailable error. I developed the algorithm for the blog as shown below.
Tables:
blogs - This table is used to store lightweight data of the post like title, featured image, URL, etc.
posts - This table is used to store the actual content of the post. It contains three columns like blog id, content, order. Here I am
using text datatype for content which will accept nearly 70k characters.
Algorithm:
When I submit a blog post, first it will create a row in the blogs table with lightweight data. And after that, the actual post content will be split into an array with each item contains 65k characters. And every item will be stored in the posts table as a new row with the blog id created in the blogs table. At the time of retrieving the post, it will join the rows in the posts table and will display the actual post.
Note: The above process is working fine without any issues.
Problem:
The actual problem is suddenly it started to show 503 error when I try to add a new post or edit the existed post with images(produces heavy amount of characters), even the post is being created in blogs table and incomplete amount of characters are adding in the posts table, while adding the rest of the content its showing 503 error.
Note: Even it is working fine on my localhost and another Bluehost server.
I tried to reduce the content splitting with 25k characters, but the result is showing the same.
if($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = 'post_' . time() . '.' .$extension;
$path = public_path('uploads/posts/' . $fileNameToStore);
if(!file_exists(public_path('uploads/posts/'))) {
mkdir(public_path('uploads/posts/'), 0777);
}
Image::make($request->file('image')->getRealPath())->resize(900, NULL)->save($path);
}else {
$fileNameToStore = NULL;
}
if($request->hasFile('author_image')) {
$filenameWithExt = $request->file('author_image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('author_image')->getClientOriginalExtension();
$authorImage = 'author_' . time() . '.' .$extension;
$path = public_path('uploads/authors/' . $authorImage);
if(!file_exists(public_path('uploads/authors/'))) {
mkdir(public_path('uploads/authors/'), 0777);
}
Image::make($request->file('author_image')->getRealPath())->resize(50, 50)->save($path);
}else {
$authorImage = NULL;
}
$blog = Blog::create([
'title' => $request->title,
'image' => $fileNameToStore,
'category' => $request->category,
'meta_description' => $request->meta_description,
'meta_keywords' => $request->meta_keywords,
'url' => $request->url,
'meta_title' => $request->meta_title,
'author_name' => $request->author_name,
'author_image' => $authorImage,
'author_linkedin' => $request->author_linkedin,
'popular' => $request->popular
]);
$contents = str_split($request->post, 65000);
$i = 1;
foreach($contents as $key => $item)
{
Post::create([
'post' => $blog->id,
'content' => $item,
'order' => $i
]);
$i++;
}
I expect the output to be redirect back to blogs page with success message "Post has been created successfully", but the actual result is
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
It looks like you need to increase values of post_max_size and upload_max_filesize in php.ini.
Guide for AWS: https://aws.amazon.com/ru/premiumsupport/knowledge-center/wordpress-themes-2mb/
I also recommend using transactions for your case. This will help to avoid partially created posts. https://laravel.com/docs/5.8/database#database-transactions
Related
My Project Works Perfectly On Local Server But After I Have Upload It To Public Server . All Add Images Functions Give Me Same Error
Can't write image data to path (upload/....
I Am Using Laravel 8
here is my code when i add brand image
$image = $request->file('brand_image');
$name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(300,300)->save('upload/brand/'.$name_gen);
$save_url = 'upload/brand/'.$name_gen;
Brand::insert([
'brand_name_en' => $request->brand_name_en,
'brand_name_ar' => $request->brand_name_ar,
'brand_slug_en' => strtolower(str_replace(' ', '-',$request->brand_name_en)),
'brand_slug_ar' => str_replace(' ', '-',$request->brand_name_ar),
'brand_image' => $save_url,
If you haven't created your path's folder, you will get that error. You need to create first it.
Good afternoon everyone, I have run across a weird issue which I can't put my finger on and I am hoping somebody can help me figure out what is causing this problem.
To provide some context, I allow the user to store an array of images on a product, and after they are stored using laravel-stapler package which is configured in the following way:
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->hasAttachedFile('image', [
'styles' => [
'thumbnail' => '500x500#',
'large' => '800x800#'
],
'url' => '/media/image/:id/:style/:filename',
'default_url' => '/img/category-placeholder-greyscale.jpg',
'convert_options' => [
'jpeg_quality' => 60
]
]);
}
The images are saved in three folders:
../path-to-image/original/file-name
../path-to-image/thumbnail/file-name
../path-to-image/large/file-name
After I save these images I use croppie.js to rotate and edit them. After the user edits and crops them and they submit it the image is sent as a base64 to a controller and the controller is shortened below to only the relevant parts:
$imageData = $request->get('imagebase64');
list(, $imageData) = explode(';', $imageData);
list(, $imageData) = explode(',', $imageData);
$imageData = base64_decode($imageData);
// $image is loaded up through dependency injection
$path = public_path('/path' . '/large/' . $image->image_file_name);
$path2 = public_path('/path' . '/original/' . $image->image_file_name);
file_put_contents($path, $imageData);
file_put_contents($path2, $imageData);
This works on my local machine just fine, the image is saved in both folders and I get a new cropped and edited image, but on my server this doesn't work, the first file_put_contents doesn't work and doesn't store a new image into the /large folder but the second file_put_contents works and stores a new image into the /original folder.
I am not sure why does this happen and would appreciate any help you can give me. I also do not think it is due to permissions because I gave the folder for the images the right permission but I can't be certain. The code doesn't crash also it just executes without saving the first image
you might have to edit the .htaccess file to give permissions to these files
Let's say i have a list of 4 images and i'm trying to randomly show 2 of them each time the newsletter is loaded.
I have a file show_image.php with the following code:
$images = array(
0 => array(
'image' => 'http://example.com/img/partner1.jpg',
'link' => 'http://www.example1.com'
),
1 => array(
'image' => 'http://example.com/img/partner2.jpg',
'link' => 'http://www.example2.com'
),
2 => array(
'image' => 'http://example.com/img/partner3.jpg',
'link' => 'http://www.example3.com'
),
3 => array(
'image' => 'http://example.com/img/partner4.jpg',
'link' => 'http://www.example4.com'
)
);
$i = 0
foreach($images as $image)
{
$i++;
$zones[$i][] = $image;
if($i == 2)
$i = 0;
}
if(!empty($zones[$_GET['zone']]))
{
$zone = $zones[$_GET['zone']];
$random_index = array_rand($zone);
$partner = $zone[$random_index];
if($_GET['field'] == 'image')
{
$file = getFullPath($partner['image']);
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}
elseif($_GET['field'] == 'link')
{
wp_redirect( $partner['link'], 301);
exit();
}
}
In my current situation, the images in the (html) newsletter template look like this:
<a href="http://example.com/show_image.php?zone=1&field=link">
<img src="http://example.com/show_image.php?zone=1&field=image">
</a>
<a href="http://example.com/show_image.php?zone=2&field=link">
<img src="http://example.com/show_image.php?zone=2&field=image">
</a>
As you can see, the call for a random image and link are separate, causing the php script to respond with a random link that doesn't match the random image.
Can anyone point me in the right direction how to randomly show an image with the right corresponding link?
First, there is a syntax error in your code. All your child arrays are missing a comma:
0 => array(
'image' => 'http://example.com/img/partner1.jpg' // <-- Error
'link' => 'http://www.example1.com'
)
Should be:
0 => array(
'image' => 'http://example.com/img/partner1.jpg', // <-- Fixed
'link' => 'http://www.example1.com'
)
You should use rand() to get an image randomly:
$images = array(
0 => array(
'image' => 'http://example.com/img/partner1.jpg',
'link' => 'http://www.example1.com'
),
1 => array(
'image' => 'http://example.com/img/partner2.jpg',
'link' => 'http://www.example2.com'
),
2 => array(
'image' => 'http://example.com/img/partner3.jpg',
'link' => 'http://www.example3.com'
),
3 => array(
'image' => 'http://example.com/img/partner4.jpg',
'link' => 'http://www.example4.com'
)
);
$total_images = count($images) - 1; // Get total number of images. Deducted one because arrays are zero-based
$random_img = rand(0, $total_images); // Get a random number between 0 and $total_images
echo $images[$random_img]['image'] . '<br />';
echo $images[$random_img]['link'] . '<br />';
there are could be multiple solutions, all of them have positive and negative sides:
Instead of static html file with hard-coded links, you can generate page on fly with php, so in this way you will generate random number for each zone and output html with proper links/images
1.1. You can use iframe to load image and link form php server
If you have to use static html and javascript, you can perform ajax call to php with javascript, which again will fetch image and link and use them to generate html code (document.write or innerHTML)
You can try to use cookies or session mechanism, in this case in php code you will have branch like if number for zone is not generated yet - generate and store in cookie/session; return link or image for number from cookies/session
To modify your code for #3 you need to replace
$random_index = array_rand($zone);
with something like (writing without actual php, so syntax errors are possible):
$cook = 'zone' . $_GET['zone'];
$random_index = isset($_COOKIE[$cook]) ? $_COOKIE[$cook] : array_rand($zone);
setcookie($cook, $random_index);
note - it is up to you to put proper validation for any variables from GET or COOKIE
In case of e-mail clients - majority of them restrict execution of javascript code and don't store cookies (and from user's perspective is it very good that they do that), anyway you can try something like that:
during e-mail sending generate unique id for each e-mail sent (you can use UUID for that)
include this id into links in your template, like <img src="http://.../?..&id=UUID">
in image and click handler - you need to get id from url and check in database - whether you assigned value to it and if no - generate and store in db
if value in db present - you can now serve appropriate image or redirect to appropriate url
but in this scheme - users always will be presented with the same image (though different users will see different ones), to fix that you can introduce some kind of expiration, ie put timestamp in db and invalidate (regenerate) value
note - some e-mail clients can force cache of images, ignoring http headers, thus such scheme will fail
other notes:
don't forget about no-cache http headers for serving image
don't use permanent redirects, only temporary ones for your use case
some e-mail clients will not load images which are not embedded into message, for such ones you can play with <noscript> and embedded images of some single randomly picked ad
Hoping I can get some help here. I am able to upload image files via the API, and I receive a file_id as a response, but every time I try to update an image field in an item using the id that was returned I get:
"File with mimetype application/octet-stream is not allowed, must match one of (MimeTypeMatcher('image', ('png', 'x-png', 'jpeg', 'pjpeg', 'gif', 'bmp', 'x-ms-bmp')),)
I've even added a line of code into my PHP script to pull the jpeg from file, rewrite it as a jpeg to be certain ( imagejpeg()) before uploading. Still, when I get to the point of updating the image field on the item, I get the same error. It seems all images uploaded via the API are converted to octet-stream. How do I get around this?
I'm using the Podio PHP library.
The PHP code is as follows:
$fileName = "testUpload.jpeg";
imagejpeg(imagecreatefromstring(file_get_contents($fileName)),$fileName);
$goFile = PodioFile::upload($fileName,$itemID);
$fileID = $goFile->file_id;
PodioItem::update((int)$itemID, array(
'fields' => array(
"logo" => (int)$fileID,
)
) , array(
"hook" => 0
));
Please try and replace :
$goFile = PodioFile::upload($fileName,$itemID);
with something like:
$goFile = PodioFile::upload('/path/to/example/file/example.jpg', 'example.jpg');
$fileID = $goFile->file_id;
PodioItem::update((int)$itemID, array(
'fields' => array(
"logo" => array((int)$fileID),
)
As it is described in https://developers.podio.com/examples/files#subsection_uploading
And then use $fileID as you've used. And yes, filename should have file extension as well, so it will not work with just 123123123 but should work well with 123123123.jpg
I'm using Drupal 6.x and I'm writing a php class that will grab an RSS feed and insert it as nodes.
The problem I'm having is that the RSS feed comes with images, and I cannot figure out how to insert them properly.
This page has some information but it doesn't work.
I create and save the image to the server, and then I add it to the node object, but when the node object is saved it has no image object.
This creates the image object:
$image = array();
if($first['image'] != '' || $first['image'] != null){
$imgName = urlencode('a' . crypt($first['image'])) . ".jpg";
$fullName = dirname(__FILE__) . '/../../sites/default/files/rssImg/a' . $imgName;
$uri = 'sites/default/files/rssImg/' . $imgName;
save_image($first['image'], $fullName);
$imageNode = array(
"fid" => 'upload',
"uid" => 1,
"filename" => $imgName,
"filepath" => $uri,
"filemime" => "image/jpeg",
"status" => 1,
'filesize' => filesize($fullName),
'timestamp' => time(),
'view' => '<img class="imagefield imagefield-field_images" alt="' . $first['title'] . '" src="/' . $uri . '" /> ',
);
$image = $imageNode;
}
and this adds the image to the node:
$node->field_images[] = $image;
I save the node using
module_load_include('inc', 'node', 'node.pages');
// Finally, save the node
node_save(&$node);
but when I dump the node object, it no longer as the image. I've checked the database, and an image does show up in the table, but it has no fid. How can I do this?
Why not get the Feedapi, along with feedapi mapper, and map the image from the RSS field into an image CCK field you create for whatever kind of node you're using?
It's very easy, and you can even do it with a user interface.
Just enable feedapi, feedapi node (not sure on the exact name of that module), feedapi mapper, and create a feed.
Once you've created the feed, go to map (which you can do for the feed content type in general as well) on the feed node, and select which feed items will go to which node fields.
You'll want to delete your feed items at this point if they've already been created, and then refresh the feed. That should be all you need to do.
Do you need to create a separate module, or would an existing one work for you?
Are you using hook_nodeapi to add the image object to the node object when $op = 'prepare'?
If you read the comments on the site you posted, you'll find:
To attach a file in a "File" field
(i.e. the field type supplied by
FileField module) you have to add a
"description" item to the field array,
like this:
$node->field_file = array(
array(
'fid' => 'upload',
'title' => basename($file_temp),
'filename' => basename($file_temp),
'filepath' => $file_temp,
'filesize' => filesize($file_temp),
'list' => 1, // always list
'filemime' => mimedetect_mime($file_temp),
'description' => basename($file_temp),// <-- add this
), );
maybe that works. You could also try to begin the assignment that way:
$node->field_file = array(0 => array(...
If you are just grabbing content wich has images in, why not just in the body of your nodes change the path of the images to absolute rather than local, then you won't have to worry about CCK at all.
Two other things that you may need to do
"fid" => 'upload', change this to the fid of the file when you save it. If it isn't in the files table then that is something you will need to fix. I think you will need feild_file_save_file()
2.you may need an array "data" under your imageNode array with "title", "description" and "alt" as fields.
To build file information, if you have filefield module, you can also use field_file_load() or field_file_save_file(). See file filefield/field_file.inc for details.
Example:
$node->field_name[] = field_file_load('sites/default/files/filename.gif');