Welcome Guest, Not a member yet? Register   Sign In
Vairable Question
#1

(This post was last modified: 04-15-2015, 06:26 AM by wolfgang1983.)

Hello

I have a function and its called delete. When i delete my file it deletes it from two places, image/cache and another the saved cached image displays like a&t_logo-100x100.jpg 100x100 means width and height.

currently on this line $cached_image = FCPATH . 'image/cache/' . utf8_substr($path, 0, utf8_strrpos($path, '.')) . '-' . 100 . 'x' . 100 . '.' . $extension;

I have to enter the width and height manually.

I would like to be able to do this $width & $height replacing the manually entered width and height.

$cached_image = FCPATH . 'image/cache/' . utf8_substr($path, 0, utf8_strrpos($path, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

How can I make it so when I enter in $width & $height variables that it would pick width and height up automatic.

utf8 is a custom helper that's autoloaded

PHP Code:
<?php

class File extends MX_Controller {
    public function 
delete() {
        
$json = array();

        if (!
$json) {
            
$path_post $this->input->post('path');
            if (isset(
$path_post)) {
                foreach (
$path_post as $path) {
                    
                    
unlink(FCPATH '/image/' $path);
                    
                    
rmdir(FCPATH '/image/' $path);
                    
                    
$extension pathinfo($pathPATHINFO_EXTENSION);

                    
//$cached_image = FCPATH . 'image/cache/' . utf8_substr($path, 0, utf8_strrpos($path, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
                    
                    
$cached_image FCPATH 'image/cache/' utf8_substr($path0utf8_strrpos($path'.')) . '-' 100 'x' 100 '.' $extension;    
    
                    
unlink($cached_image);
                }
            }

            foreach (
$path_post as $path) {
                
$json['success'] = $this->lang->line('text_delete');
            }
        }

        
$this->output->set_content_type('Content-Type: application/json');
        
$this->output->set_output(json_encode($json));
    }



In stead of having the width and height in image name should I just have it normal a&t_logo.jpg That I can do what would your thoughts be on that?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

I don't understand why you're doing it this way if they are copies of the same image in different directories. Since they're DIFFERENT, the filename can be the same in both and you don't need to mess with that.

$image = 'some-image.png';

unlink('/path/to/regular/images/' . $image);
unlink('/path/to/cached/images/' . $image);

But to answer your question, can't you just use image functions to get the width/height of the image before you delete it?

PHP Code:
foreach ($path_post as $path) {
  
$file_name FCPATH '/image/' $path;

  
//check to make sure image exists
  
if (file_exists($file_name))
  {
    
//get height/width of image
    
list($height$width) = getimagesize($file_name);

    
//build the cached image file name
    
$extension pathinfo($pathPATHINFO_EXTENSION);
    
$cached_image FCPATH 'image/cache/' utf8_substr($path0utf8_strrpos($path'.')) . '-' $width 'x' $height '.' $extension;

    
//delete cached image
    
unlink($cached_image);

    
//delete regular image
    
unlink($file_name);
  }    

Reply
#3

(04-15-2015, 07:53 AM)CroNiX Wrote: I don't understand why you're doing it this way if they are copies of the same image in different directories. Since they're DIFFERENT, the filename can be the same in both and you don't need to mess with that.

$image = 'some-image.png';

unlink('/path/to/regular/images/' . $image);
unlink('/path/to/cached/images/' . $image);

But to answer your question, can't you just use image functions to get the width/height of the image before you delete it?


PHP Code:
foreach ($path_post as $path) {
 
 $file_name FCPATH '/image/' $path;

 
 //check to make sure image exists
 
 if (file_exists($file_name))
 
 {
 
   //get height/width of image
 
   list($height$width) = getimagesize($file_name);

 
   //build the cached image file name
 
   $extension pathinfo($pathPATHINFO_EXTENSION);
 
   $cached_image FCPATH 'image/cache/' utf8_substr($path0utf8_strrpos($path'.')) . '-' $width 'x' $height '.' $extension;

 
   //delete cached image
 
   unlink($cached_image);

 
   //delete regular image
 
   unlink($file_name);
 
    


I will look more into it. Thanks for pointing it out. All ways happy to learn.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB