Add chmod of file just made to php script

I have a wordpress plugin that resizes images to a specific size as they are uploaded without without cropping/stretching. ( https://wordpress.org/plugins/jresizr/ ).

A copy of the script is below - I need to somehow add that the images need to have their permissions set to read only, chmod 0444.

I haven't been able to figure out how to do that or where it would go.

Can you take a look at the script below and show me how/where to chmod?

Thanks,

Chris


<?php
/*
	Plugin Name: Jresizr
	Plugin URI: http://jegtheme.com/
	Description: Give abilty to uncrop image and put background color behind the image.
	Version: 1.0.0
	Author: Agung Bayu Iswara
	Author URI: http://jegtheme.com
	License: GPL2
*/

/*  
	Copyright 2012  Agung Iswara  (email : agungbayuiswara@gmail.com)
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License, version 2, as 
	published by the Free Software Foundation.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once ( dirname(__FILE__) . '/jpanel.php');

if ( !class_exists( 'JResizr' ) ) :

	class JResizr extends JPanel {
				
		/**
		 * @since 1.0.0
		 * @var JPanel
		 */
		private $jpanel;		
		
		/**
		 * constructor
		 * @since 1.0.0		 
		 */
		public function __construct() 
		{
			$this->plugin_init();				
		}
				
		/**
		 * setup plugin
		 * @since 1.0.0
		 * @return void
		 */		
		public function plugin_init () 
		{
			$this->jpanel_init();
			register_activation_hook( __FILE__, array( &$this, 'activation' ) );
			
			if( $this->get_option('enablejresizr') ) {
				add_filter('wp_generate_attachment_metadata', array(&$this, 'generate_post_thumbnail'), 10, 2);
			}
		}
				
		/**
		 * 
		 * @since 1.0.0
		 * @return void
		 */
		public function generate_post_thumbnail ($metadata, $attachment_id) 
		{
			$file = $metadata['file'];
			
			//define upload path & dir
			$upload_info = wp_upload_dir();
			$upload_dir = $upload_info['basedir'];
			$upload_url = $upload_info['baseurl'];
			
			// get the real directory and file path
			$filepath 		= $upload_dir . DIRECTORY_SEPARATOR . $file;
			$info 			= pathinfo($filepath );
			$filedir 		= $info['dirname'];			
			$fillcolor 		= $this->get_option('colorjresizr', '000000');
						
			// delete previous file
			foreach ($metadata['sizes'] as $size) {
				$dest = $filedir . DIRECTORY_SEPARATOR . $size['file'];
				if (file_exists($dest)) {
					unlink($dest);
				}
			}
			
			// empty sizes file meta data
			$metadata['sizes'] = null;
			
			// make thumbnails and other intermediate sizes
			global $_wp_additional_image_sizes;
						
			foreach ( get_intermediate_image_sizes() as $s ) {
				$sizes[$s] = array( 'width' => '', 'height' => '');
				
				/** width **/
				if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
					$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
				else
					$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
				
				/** height **/
				if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
					$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
				else
					$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
				
				/** file name **/
				$suffix = "{$sizes[$s]['width']}x{$sizes[$s]['height']}";
				
				$info = pathinfo($filepath);
				$dir = $info['dirname'];
				$ext = $info['extension'];
				$name = wp_basename($file, ".$ext");
				
				$sizes[$s]['file'] = "{$dir}/{$name}-{$suffix}.{$ext}";
			}
			
			// resize actual file
			foreach ($sizes as &$size_data ) {
				$this->resize_nocrop( $filepath, $size_data['file'], $size_data['width'], $size_data['height'] , 100,  $fillcolor);
				$size_data['file'] = wp_basename($size_data['file']);
			}
			
			$metadata['sizes'] = $sizes;
			return $metadata;
		}
		
		public function resize_nocrop ($source_image, $destination, $width, $height, $quality = 100, $fillcolor) 
		{
			$info = getimagesize($source_image);
		    $imgtype = image_type_to_mime_type($info[2]);
			
		    switch ($imgtype) {
		        case 'image/jpeg':
		            $source = imagecreatefromjpeg($source_image);
		            break;
		        case 'image/gif':
		            $source = imagecreatefromgif($source_image);
		            break;
		        case 'image/png':
		            $source = imagecreatefrompng($source_image);
		            break;
		        default:
		        	return;
		    }
		    
		    #Figure out the dimensions of the image and the dimensions of the desired thumbnail
		    $src_w = imagesx($source);
		    $src_h = imagesy($source);
			
		    #Do some math to figure out which way we'll need to crop the image
		    #to get it proportional to the new size, then crop or adjust as needed
		    $x_ratio = $width  / $src_w;
		    $y_ratio = $height / $src_h;
		    
			if (($src_w <= $width) && ($src_h <= $height)) {
		        $new_w = $src_w;
		        $new_h = $src_h;
		    } elseif (($x_ratio * $src_h) < $height) {
		        $new_h = ceil($x_ratio * $src_h);
		        $new_w = $width;
		    } else {
		        $new_w = ceil($y_ratio * $src_w);
		        $new_h = $height;
		    }
		    
		    $newpic = imagecreatetruecolor(round($new_w), round($new_h));
		    imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
		    $final = imagecreatetruecolor($width, $height);
		    
		    $fillbg = $this->j_hex2RGB($fillcolor);
		    $backgroundColor = imagecolorallocate($final, $fillbg['red'], $fillbg['green'], $fillbg['blue']);
		    imagefill($final, 0, 0, $backgroundColor);
		    imagecopy($final, $newpic, (($width - $new_w)/ 2), (($height - $new_h) / 2), 0, 0, $new_w, $new_h);
		       
		    if (imagejpeg($final, $destination, $quality)) {
		        return true;
		    }
		    return false;
		}
		
		/**
		 * Handles activation tasks, such as registering the uninstall hook.
		 * @since 1.0.0
		 * @return void
		 */
		public function activation() 
		{
			// delete option for activated first
			delete_option( $this->jps->get_pluginoption() );
		}
		
		/**
		 * Handles uninstallation tasks, such as deleting plugin options.
		 * @since 1.0.0
		 * @return void
		 */
		public function uninstall() 
		{
			delete_option( $this->jps->get_pluginoption() );
		}
		
			
		/**
		 * Setup JPSetting value
		 * @since 1.0.0
		 * @return JPSetting
		 */
		public function jps_init() 
		{
			// meta setting for this plugin			
			return new JPSetting( $this->file_basename(__FILE__)	, 
				$this->file_basename(__FILE__) . '_option' , 
				__FILE__ , '1.0.0', $this->get_panel() );
		}
		
		/**
		 * Get panel option used for this plugin
		 * @since 1.0.0
		 * return Array
		 */
		public function get_panel () 
		{
			$panel = array(
				array(
					'type'			=> 'heading',
					'title'			=> 'Jresizr Option'
				),
				array (
					'id' 			=> 'enablejresizr',
					'type'			=> 'switchtoogle',
					'title'			=> 'Enable JResizr',
					'description'	=> __('Turn on jresizr, non croping image with filled background will take affect after you turn on this option. '),
					'value'			=> $this->get_option('enablejresizr', 0)
				),
				array (
					'id' 			=> 'colorjresizr',
					'type'			=> 'colorpicker',
					'title'			=> 'Background Color for Jresizr',
					'description'	=> __('Set background color for jresizr.'),
					'value'			=> $this->get_option('colorjresizr', '000000')
				)
			);
			return $panel;
		}
		
		
	}
endif;

/*
 * make sure we running this plugin inside admin dashboard
 */ 
if ( is_admin() ) {
	$jresizr = new JResizr();
}

Open in new window

St_Aug_Beach_BumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You may need to experiment a little to get the file path correct -- it's a server path, not a URL path.  Once you get that part, this will set the chmod() but I'm not sure you want 0444 - you might want 0755 instead.
$msg = NULL;
if (!chmod ($my_path, 0444))
{
    $msg .= "<br/>chmod(0444) FAILED: fileperms() = ";
    $msg .= substr(sprintf('%o', fileperms($my_path)), -4);
}
echo $msg;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
St_Aug_Beach_BumAuthor Commented:
Hi Ray,

Thank you for your help.

This looks like it will tell me if it is set to 0444 or not, but not actually set it, or am I misunderstanding something?

What I need is to chmod the files the script makes, so the permissions are set to what I need them to be,

Chris
F PCommented:
Change this function:

<?php

public function resize_nocrop ($source_image, $destination, $width, $height, $quality = 100, $fillcolor) 
{
    $info = getimagesize($source_image);
    $imgtype = image_type_to_mime_type($info[2]);
      
    switch ($imgtype) {
        case 'image/jpeg':
            $source = imagecreatefromjpeg($source_image);
            break;
        case 'image/gif':
            $source = imagecreatefromgif($source_image);
            break;
        case 'image/png':
            $source = imagecreatefrompng($source_image);
            break;
        default:
              return;
    }
    
    #Figure out the dimensions of the image and the dimensions of the desired thumbnail
    $src_w = imagesx($source);
    $src_h = imagesy($source);
      
    #Do some math to figure out which way we'll need to crop the image
    #to get it proportional to the new size, then crop or adjust as needed
    $x_ratio = $width  / $src_w;
    $y_ratio = $height / $src_h;
    
      if (($src_w <= $width) && ($src_h <= $height)) {
        $new_w = $src_w;
        $new_h = $src_h;
    } elseif (($x_ratio * $src_h) < $height) {
        $new_h = ceil($x_ratio * $src_h);
        $new_w = $width;
    } else {
        $new_w = ceil($y_ratio * $src_w);
        $new_h = $height;
    }
    
    $newpic = imagecreatetruecolor(round($new_w), round($new_h));
    imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
    $final = imagecreatetruecolor($width, $height);
    
    $fillbg = $this->j_hex2RGB($fillcolor);
    $backgroundColor = imagecolorallocate($final, $fillbg['red'], $fillbg['green'], $fillbg['blue']);
    imagefill($final, 0, 0, $backgroundColor);
    imagecopy($final, $newpic, (($width - $new_w)/ 2), (($height - $new_h) / 2), 0, 0, $new_w, $new_h);

    if (imagejpeg($final, $destination, $quality)) {
        $perms = substr(decoct(fileperms($destination . $final)),3);
        if((int) $perms !== 444)
        {
            chmod($destination . $final, 0444);
        }
        
        return true;
    }
    return false;
}

Open in new window

Ray PaseurCommented:
What I need is to chmod the files the script makes, so the permissions are set to what I need them to be,
OK, so what do you want them to be?  In the original post you said 0444, right?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.