Laravel 5.5 + image not readable error (used Intervention\Image) - php

I used ImageOptimizer package for reducing image size. Source: http://image.intervention.io/getting_started/installation
In controller:
if (Input::hasFile('title_image')) {
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
How can I fix this error???:
image not readable

Use getRealPath() when you upload an image.
if (Input::hasFile('title_image')) {
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product->getRealPath())->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}

Related

Laravel 8 - Upload storage path changed, how to show old files?

In laravel 8, I updated the storage path for image uploads. It was app/violations/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/', $filename);
$violation->violationStatement = $filename;
}
And it was updated to app/violations/speeding/Toyota/XXX-1234/11-June-2022/11-26 AM/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = 'violation-statement' . '-' . $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/' .
$violation->violationType . '/' .
$violation->carModel . '/' .
$violation->plateNumber . '/' .
Carbon::parse($violation->violationDateTime)->format('d-M-Y/H-i A'), $filename);
$violation->violationStatement = $filename;
}
The images are displayed in datatables, so obviously I'll have to change the image src to the new URL. When I do so, how can I also display the old images of the old directory?

Edit an XML within compressed tgz file without extracting it

I have been trying to modify an XML document within an archived .tgz file by using the PharData class using php. I have no problem modifying the XML through DomDocument class, but I am having a hard time saving the whole process: I end up saving 2 different .phar documents alltogether, w/o modifying anything in the .tgz archive :
folder example image
Here is the code I have been using :
$phar = new PharData($path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file);
echo $phar . "\n";
$dom = new DOMDocument();
$dom->load ('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '/' . substr($file, 0, -4) . '.xml');
if ($dom === null) continue;
$ids = $dom->documentElement->getElementsByTagName('id');
foreach ($ids as $i_id) {
if ($i_id->nodeValue !== $id) continue;
echo $i_id->nodeValue . "\n";
echo $i_id->nodeName . "\n";
$sensor = $i_id->parentNode;
echo $sensor->nodeName . "\n";
foreach ($sensor->childNodes as $object) {
if ($object->nodeName !== 'type') continue;
echo $object->nodeName . "\n";
echo $object->nodeValue . "\n";
$object->nodeValue = $type;
echo $object->nodeValue . "\n" . "\n";
}
}
$dom->save('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '.phar' . '/' . substr($file, 0, -4) . '.xml');

I want to upload 2 files on 2 differnt path on same form using Zend framework 2

My code is below. Only 2nd file (shareholder1) is getting upload, first file is not getting upload.The problem is with AddFilter..
$upload = new Zend_File_Transfer();
$company_name = $post['idcmpname'];
$douments_path = realpath('companydocuments');
$current_yr = date("Y");
$yr8 = 2018;
$company_name = str_replace(' ', '_', $company_name);
$path_memorandum = $douments_path . '/' . $company_name . '/' . 'memorandum' . '/' . $current_yr;
if (!is_dir($path_memorandum)) {
mkdir($path_memorandum, 0777, true);
}
$path_shareholder1 = $douments_path . '/' . $company_name . '/' . 'shareholder/' . $yr8;
if (!is_dir($path_shareholder1)) {
mkdir($path_shareholder1, 0777, true);
$filename_memorandum = str_replace(' ', '_', $files['memorandum1']['name']);
$tmpName = $files['memorandum1']['tmp_name'];
$filename_shareholder1 = str_replace(' ', '_', $files['shareholders1']['name']);
$tmpName = $files['shareholders1']['tmp_name'];
$upload->addValidator('Size', false, array('max' => '10MB', 'bytestring' => false));
$upload->addValidator('Extension', false, array('jpeg', 'jpg', 'png', 'pdf', 'txt', 'xls', 'xlsx', 'ppt', 'doc', 'docx', 'rtf'));
if ($upload->isValid()) {
$upload->addFilter('Rename', $douments_path . '/' . $company_name . '/' . 'memorandum' . '/' . $current_yr . '/' . $filename_memorandum, 'memorandum1')
->addFilter('Rename', $douments_path . '/' . $company_name . '/' . 'shareholder/' . $yr8 . '/' . $filename_shareholder1, 'shareholders1');
}
}
The problem is in AddFilter..I am new to ZF

Laravel 5.4 Upload images with their own names

i want to upload my images with their own names. But when i tried, they upload with diffrent names. for example; php0K0Saj.57352.JPG (!?)
my controller;
public function post_Savenews(Request $request)
{
$request->all();
/* out of question
$head = $request->input('head');
$content = $request->input('content');
$keywords = $request->input('keywords'); */
$featured=$request->post_featured;
$extension=$request->post_featured->getClientOriginalExtension();
$photoName = $featured . '.' . rand(11111, 99999) . '.' . $extension;
$request->post_featured->move(public_path('uploads'), $photoName);
News::create(array('head' => $head, 'content' => $content, 'keywords' => $keywords,'post_featured'=>$photoName));
return redirect()->route('index');
}
Update the line.
$photoName = $featured . '.' . rand(11111, 99999) . '.' . $extension;
With
$photoName = $request->post_featured->getClientOriginalName();
Hope this helps

PHP rename fails but file_exists is true

I'm trying to rename all files in a folder, I want the last character before the extension to be removed.
This is the code I'm using:
$sql = 'SELECT * FROM tblphotoseries WHERE photoSerieID = ' . mysql_real_escape_string($photoSerieID) . ' AND FKuID = ' . $_SESSION['uID'];
if($series = GetFromDB($sql)){
foreach ($series as $serie){
$path = '../uploads/' . $serie["FKcatID"] . '/' . $_SESSION['uCode'] . '/' . $serie["seriesCode"] . '';
}
$files = scandir($path);
foreach($files as $file) {
if($file != ".." && $file != "."){
$new_name = explode(".", $file);
$ext = $new_name[1];
$new_name = $new_name[0];
$new_name[strlen($new_name) - 1] = "";
$new_name = $new_name . "." . $ext;
$new_name = (string)$new_name;
echo $file . " -> " . $new_name . "\n\r";
clearstatcache();
if(file_exists($path . '/' . $file)){
echo "file_exists \n\r";
clearstatcache();
if(rename($path . '/' . $file, $path . '/' . $new_name)){
echo "rename successful \n\r";
}else{
echo "rename failed \n\r";
}
}
}
}
}
This outputs the following:
AA04a_.jpg -> AA04a.jpg
file_exists
rename failed
AA04b_.png -> AA04b.png
file_exists
rename failed
AA04c_.png -> AA04c.png
file_exists
rename failed
Can anyone spot what's going wrong here? Privileges are ok, I've tried chmod 0777 on the file before renaming as well, without success.
Thanks,
Rik
SOLVED:
$new_name[strlen($new_name) - 1] = "";
had to be replaced by
$new_name = substr_replace($new_name, '', strlen($new_name) - 1, 1);

Categories