Monday, March 9, 2015

How to give Subdomain path in Jbimages plugin in tinymce

recently i got some website project that use subdomain for it's image or something like CDN for serving images, and i got some serious problem when i try to upload an image using tinymce plugin called Jbimages.

the problem is image not showing in the editor because it's relative url like '../cdn_public_html/uploads/filename.jpg', I solve this problem by editing this 2 files

tinymce/plugins/jbimages/ci/application/controllers/uploader.php
tinymce/plugins/jbimages/config.php


by adding some configuration ($conf['base_image_url']) and setting that to the controller (function upload), so the output (ajax) is prepended with your sub domain or your CDN url.
see my Gist below for how to do this

<?php
/* tinymce/plugins/jbimages/config.php */
| -------------------------------------------------------------------
|
| Path to upload target folder, relative to domain name. NO TRAILING SLASH!
| Example: if an image is acessed via http://www.example.com/images/somefolder/image.jpg, you should specify here:
|
| $config['img_path'] = '/images/somefolder';
|
| -------------------------------------------------------------------*/
// $config['img_path'] = '/images'; // Relative to domain name
// $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . $config['img_path']; // Physical path. [Usually works fine like this]
$config['img_path'] = 'images/uploads';
$config['base_image_url'] = 'http://cdn.yourdomain.com/';
$config['relative_path'] = '/../cdn_public_html/';
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . $config['relative_path'] . $config['img_path'];
view raw config.php hosted with ❤ by GitHub
<?php
/* tinymce/plugins/jbimages/ci/application/controllers/uploader.php */
public function upload ($lang='english')
{
// Set language
$this->_lang_set($lang);
// Get configuartion data (we fill up 2 arrays - $config and $conf)
$conf['base_image_url'] = $this->config->item('base_image_url', 'uploader_settings');
$conf['img_path'] = $this->config->item('img_path', 'uploader_settings');
$conf['allow_resize'] = $this->config->item('allow_resize', 'uploader_settings');
$config['allowed_types'] = $this->config->item('allowed_types', 'uploader_settings');
$config['max_size'] = $this->config->item('max_size', 'uploader_settings');
$config['encrypt_name'] = $this->config->item('encrypt_name', 'uploader_settings');
$config['overwrite'] = $this->config->item('overwrite', 'uploader_settings');
$config['upload_path'] = $this->config->item('upload_path', 'uploader_settings');
if (!$conf['allow_resize'])
{
$config['max_width'] = $this->config->item('max_width', 'uploader_settings');
$config['max_height'] = $this->config->item('max_height', 'uploader_settings');
}
else
{
$conf['max_width'] = $this->config->item('max_width', 'uploader_settings');
$conf['max_height'] = $this->config->item('max_height', 'uploader_settings');
if ($conf['max_width'] == 0 and $conf['max_height'] == 0)
{
$conf['allow_resize'] = FALSE;
}
}
// Load uploader
$this->load->library('upload', $config);
if ($this->upload->do_upload()) // Success
{
// General result data
$result = $this->upload->data();
// Shall we resize an image?
if ($conf['allow_resize'] and $conf['max_width'] > 0 and $conf['max_height'] > 0 and (($result['image_width'] > $conf['max_width']) or ($result['image_height'] > $conf['max_height'])))
{
// Resizing parameters
$resizeParams = array
(
'source_image' => $result['full_path'],
'new_image' => $result['full_path'],
'width' => $conf['max_width'],
'height' => $conf['max_height']
);
// Load resize library
$this->load->library('image_lib', $resizeParams);
// Do resize
$this->image_lib->resize();
}
// Add our stuff
$result['result'] = "file_uploaded";
$result['resultcode'] = 'ok';
$result['file_name'] = $conf['base_image_url'] . $conf['img_path'] . '/' . $result['file_name'];
// Output to user
$this->load->view('ajax_upload_result', $result);
}
else // Failure
{
// Compile data for output
$result['result'] = $this->upload->display_errors(' ', ' ');
$result['resultcode'] = 'failed';
// Output to user
$this->load->view('ajax_upload_result', $result);
}
}
view raw uploader.php hosted with ❤ by GitHub

1 comment: